Module Name:    src
Committed By:   maxv
Date:           Sun Aug 12 12:42:54 UTC 2018

Modified Files:
        src/sys/arch/amd64/amd64: machdep.c prekern.c
        src/sys/arch/amd64/include: pmap.h
        src/sys/arch/amd64/stand/prekern: pdir.h prekern.c
        src/sys/arch/x86/x86: pmap.c

Log Message:
Move the PTE area from slot 255 to slot 509. I've never understood why we
put it on 255; the "kernel" half of the VM space begins on slot 256, so
if anything, the PTE area should have been above it, not below.

Virtually extend the user slots in slotspace, because we don't want
(randomized) kernel mappings to land on slot 255.

The prekern is updated accordingly.

Tested on GENERIC, GENERIC_KASLR and XEN3_DOM0.


To generate a diff of this commit:
cvs rdiff -u -r1.312 -r1.313 src/sys/arch/amd64/amd64/machdep.c
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/amd64/prekern.c
cvs rdiff -u -r1.51 -r1.52 src/sys/arch/amd64/include/pmap.h
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/pdir.h
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/amd64/stand/prekern/prekern.c
cvs rdiff -u -r1.300 -r1.301 src/sys/arch/x86/x86/pmap.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/sys/arch/amd64/amd64/machdep.c
diff -u src/sys/arch/amd64/amd64/machdep.c:1.312 src/sys/arch/amd64/amd64/machdep.c:1.313
--- src/sys/arch/amd64/amd64/machdep.c:1.312	Sun Aug 12 10:50:35 2018
+++ src/sys/arch/amd64/amd64/machdep.c	Sun Aug 12 12:42:53 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: machdep.c,v 1.312 2018/08/12 10:50:35 maxv Exp $	*/
+/*	$NetBSD: machdep.c,v 1.313 2018/08/12 12:42:53 maxv Exp $	*/
 
 /*
  * Copyright (c) 1996, 1997, 1998, 2000, 2006, 2007, 2008, 2011
@@ -110,7 +110,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.312 2018/08/12 10:50:35 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.313 2018/08/12 12:42:53 maxv Exp $");
 
 #include "opt_modular.h"
 #include "opt_user_ldt.h"
@@ -1597,10 +1597,10 @@ init_slotspace(void)
 
 	memset(&slotspace, 0, sizeof(slotspace));
 
-	/* User. */
+	/* User. [256, because we want to land in >= 256] */
 	slotspace.area[SLAREA_USER].sslot = 0;
-	slotspace.area[SLAREA_USER].mslot = PDIR_SLOT_PTE;
-	slotspace.area[SLAREA_USER].nslot = PDIR_SLOT_PTE;
+	slotspace.area[SLAREA_USER].mslot = PDIR_SLOT_USERLIM+1;
+	slotspace.area[SLAREA_USER].nslot = PDIR_SLOT_USERLIM+1;
 	slotspace.area[SLAREA_USER].active = true;
 	slotspace.area[SLAREA_USER].dropmax = false;
 

Index: src/sys/arch/amd64/amd64/prekern.c
diff -u src/sys/arch/amd64/amd64/prekern.c:1.3 src/sys/arch/amd64/amd64/prekern.c:1.4
--- src/sys/arch/amd64/amd64/prekern.c:1.3	Thu Aug  2 17:18:00 2018
+++ src/sys/arch/amd64/amd64/prekern.c	Sun Aug 12 12:42:53 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.c,v 1.3 2018/08/02 17:18:00 maxv Exp $	*/
+/*	$NetBSD: prekern.c,v 1.4 2018/08/12 12:42:53 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -46,7 +46,7 @@
 #include <dev/isa/isareg.h>
 #include <machine/isa_machdep.h>
 
-#define PREKERN_API_VERSION	1
+#define PREKERN_API_VERSION	2
 
 struct prekern_args {
 	int version;

Index: src/sys/arch/amd64/include/pmap.h
diff -u src/sys/arch/amd64/include/pmap.h:1.51 src/sys/arch/amd64/include/pmap.h:1.52
--- src/sys/arch/amd64/include/pmap.h:1.51	Sun Aug 12 12:23:33 2018
+++ src/sys/arch/amd64/include/pmap.h	Sun Aug 12 12:42:53 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.h,v 1.51 2018/08/12 12:23:33 maxv Exp $	*/
+/*	$NetBSD: pmap.h,v 1.52 2018/08/12 12:42:53 maxv Exp $	*/
 
 /*
  * Copyright (c) 1997 Charles D. Cranor and Washington University.
@@ -137,7 +137,7 @@
  */
 #define VA_SIGN_POS(va)		((va) & ~VA_SIGN_MASK)
 
-#define L4_SLOT_PTE		255
+#define L4_SLOT_PTE		509
 #define L4_SLOT_KERN		slotspace.area[SLAREA_MAIN].sslot
 #define L4_SLOT_KERNBASE	511 /* pl4_i(KERNBASE) */
 
@@ -153,7 +153,7 @@
  * PDP_PDE: the VA of the PDE that points back to the PDP
  */
 
-#define PTE_BASE	((pt_entry_t *)(L4_SLOT_PTE * NBPD_L4))
+#define PTE_BASE	((pt_entry_t *)VA_SIGN_NEG((L4_SLOT_PTE * NBPD_L4)))
 
 #define L1_BASE	PTE_BASE
 #define L2_BASE	((pd_entry_t *)((char *)L1_BASE + L4_SLOT_PTE * NBPD_L3))

Index: src/sys/arch/amd64/stand/prekern/pdir.h
diff -u src/sys/arch/amd64/stand/prekern/pdir.h:1.4 src/sys/arch/amd64/stand/prekern/pdir.h:1.5
--- src/sys/arch/amd64/stand/prekern/pdir.h:1.4	Sun Jan 21 10:59:21 2018
+++ src/sys/arch/amd64/stand/prekern/pdir.h	Sun Aug 12 12:42:54 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pdir.h,v 1.4 2018/01/21 10:59:21 maxv Exp $	*/
+/*	$NetBSD: pdir.h,v 1.5 2018/08/12 12:42:54 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -32,12 +32,12 @@
 #define PREKERNTEXTOFF	(PREKERNBASE + 0x100000)
 
 #define L4_SLOT_PREKERN	0 /* pl4_i(PREKERNBASE) */
-#define L4_SLOT_PTE	255
+#define L4_SLOT_PTE	509
 
 #define PDIR_SLOT_KERN	L4_SLOT_PREKERN
 #define PDIR_SLOT_PTE	L4_SLOT_PTE
 
-#define PTE_BASE	((pt_entry_t *)(L4_SLOT_PTE * NBPD_L4))
+#define PTE_BASE	((pt_entry_t *)VA_SIGN_NEG((L4_SLOT_PTE * NBPD_L4)))
 
 #define L1_BASE	PTE_BASE
 #define L2_BASE	((pd_entry_t *)((char *)L1_BASE + L4_SLOT_PTE * NBPD_L3))

Index: src/sys/arch/amd64/stand/prekern/prekern.c
diff -u src/sys/arch/amd64/stand/prekern/prekern.c:1.9 src/sys/arch/amd64/stand/prekern/prekern.c:1.10
--- src/sys/arch/amd64/stand/prekern/prekern.c:1.9	Thu Aug  2 17:18:00 2018
+++ src/sys/arch/amd64/stand/prekern/prekern.c	Sun Aug 12 12:42:54 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.c,v 1.9 2018/08/02 17:18:00 maxv Exp $	*/
+/*	$NetBSD: prekern.c,v 1.10 2018/08/12 12:42:54 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -221,7 +221,7 @@ init_idt(void)
 
 /* -------------------------------------------------------------------------- */
 
-#define PREKERN_API_VERSION	1
+#define PREKERN_API_VERSION	2
 
 struct prekern_args {
 	int version;

Index: src/sys/arch/x86/x86/pmap.c
diff -u src/sys/arch/x86/x86/pmap.c:1.300 src/sys/arch/x86/x86/pmap.c:1.301
--- src/sys/arch/x86/x86/pmap.c:1.300	Sun Aug 12 12:23:33 2018
+++ src/sys/arch/x86/x86/pmap.c	Sun Aug 12 12:42:54 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pmap.c,v 1.300 2018/08/12 12:23:33 maxv Exp $	*/
+/*	$NetBSD: pmap.c,v 1.301 2018/08/12 12:42:54 maxv Exp $	*/
 
 /*
  * Copyright (c) 2008, 2010, 2016, 2017 The NetBSD Foundation, Inc.
@@ -157,7 +157,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.300 2018/08/12 12:23:33 maxv Exp $");
+__KERNEL_RCSID(0, "$NetBSD: pmap.c,v 1.301 2018/08/12 12:42:54 maxv Exp $");
 
 #include "opt_user_ldt.h"
 #include "opt_lockdebug.h"
@@ -1418,7 +1418,7 @@ slotspace_rand(int type, size_t sz, size
 
 	/* Get the holes. */
 	nholes = 0;
-	size_t curslot = 0 + 255; /* end of SLAREA_USER */
+	size_t curslot = 0 + 256; /* end of SLAREA_USER */
 	while (1) {
 		/*
 		 * Find the first occupied slot after the current one.

Reply via email to