CVS commit: src/sys/arch/amd64/stand/prekern

2021-05-04 Thread Pierre Pronchery
Module Name:src
Committed By:   khorben
Date:   Tue May  4 21:13:38 UTC 2021

Modified Files:
src/sys/arch/amd64/stand/prekern: prng.c

Log Message:
prekern: add warnings upon problems collecting entropy

As submitted on port-amd64@ (part 3/3)

Tested on NetBSD/amd64.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/prng.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/stand/prekern/prng.c
diff -u src/sys/arch/amd64/stand/prekern/prng.c:1.4 src/sys/arch/amd64/stand/prekern/prng.c:1.5
--- src/sys/arch/amd64/stand/prekern/prng.c:1.4	Tue May  4 21:10:25 2021
+++ src/sys/arch/amd64/stand/prekern/prng.c	Tue May  4 21:13:38 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: prng.c,v 1.4 2021/05/04 21:10:25 khorben Exp $	*/
+/*	$NetBSD: prng.c,v 1.5 2021/05/04 21:13:38 khorben Exp $	*/
 
 /*
  * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
@@ -84,6 +84,7 @@ prng_get_entropy_file(SHA512_CTX *ctx)
 	uint8_t digest[SHA1_DIGEST_LENGTH];
 	rndsave_t *rndsave;
 	SHA1_CTX sig;
+	size_t count = 0;
 
 	biml =
 	(struct btinfo_modulelist *)prng_lookup_bootinfo(BTINFO_MODULELIST);
@@ -117,7 +118,10 @@ prng_get_entropy_file(SHA512_CTX *ctx)
 		}
 
 		SHA512_Update(ctx, rndsave->data, sizeof(rndsave->data));
+		count++;
 	}
+	if (count == 0)
+		print_state(STATE_WARNING, "No entropy file could be loaded");
 }
 
 /*
@@ -168,6 +172,8 @@ prng_init(void)
 		cpuid(0x01, 0x00, descs);
 		has_rdrand = (descs[2] & CPUID2_RDRAND) != 0;
 	}
+	if (!has_rdseed && !has_rdrand)
+		print_state(STATE_WARNING, "No CPU entropy feature detected");
 
 	SHA512_Init();
 	prng_get_entropy_file();



CVS commit: src/sys/arch/amd64/stand/prekern

2021-05-04 Thread Pierre Pronchery
Module Name:src
Committed By:   khorben
Date:   Tue May  4 21:10:25 UTC 2021

Modified Files:
src/sys/arch/amd64/stand/prekern: prng.c

Log Message:
prekern: do not choke on bad entropy files

As submitted on port-amd64@ (part 2/3)

Tested on NetBSD/amd64.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/stand/prekern/prng.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/stand/prekern/prng.c
diff -u src/sys/arch/amd64/stand/prekern/prng.c:1.3 src/sys/arch/amd64/stand/prekern/prng.c:1.4
--- src/sys/arch/amd64/stand/prekern/prng.c:1.3	Thu May 21 08:20:25 2020
+++ src/sys/arch/amd64/stand/prekern/prng.c	Tue May  4 21:10:25 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: prng.c,v 1.3 2020/05/21 08:20:25 maxv Exp $	*/
+/*	$NetBSD: prng.c,v 1.4 2021/05/04 21:10:25 khorben Exp $	*/
 
 /*
  * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
@@ -98,7 +98,9 @@ prng_get_entropy_file(SHA512_CTX *ctx)
 			continue;
 		}
 		if (bi->len != sizeof(rndsave_t)) {
-			fatal("rndsave_t size mismatch");
+			print_state(STATE_WARNING,
+	"size mismatch in entropy file");
+			continue;
 		}
 		rndsave = (rndsave_t *)(vaddr_t)bi->base;
 
@@ -109,7 +111,9 @@ prng_get_entropy_file(SHA512_CTX *ctx)
 		SHA1Update(, rndsave->data, sizeof(rndsave->data));
 		SHA1Final(digest, );
 		if (memcmp(digest, rndsave->digest, sizeof(digest))) {
-			fatal("bad SHA1 checksum");
+			print_state(STATE_WARNING,
+	"bad SHA1 checksum in entropy file");
+			continue;
 		}
 
 		SHA512_Update(ctx, rndsave->data, sizeof(rndsave->data));



CVS commit: src/sys/arch/amd64/stand/prekern

2021-05-04 Thread Pierre Pronchery
Module Name:src
Committed By:   khorben
Date:   Tue May  4 21:09:16 UTC 2021

Modified Files:
src/sys/arch/amd64/stand/prekern: console.c elf.c mm.c prekern.c
prekern.h

Log Message:
prekern: add support for warning messages

As submitted on port-amd64@ (part 1/3)

Tested on NetBSD/amd64.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/console.c
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.27 -r1.28 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/amd64/stand/prekern/prekern.c
cvs rdiff -u -r1.23 -r1.24 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/console.c
diff -u src/sys/arch/amd64/stand/prekern/console.c:1.6 src/sys/arch/amd64/stand/prekern/console.c:1.7
--- src/sys/arch/amd64/stand/prekern/console.c:1.6	Sat May 23 08:25:32 2020
+++ src/sys/arch/amd64/stand/prekern/console.c	Tue May  4 21:09:16 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: console.c,v 1.6 2020/05/23 08:25:32 maxv Exp $	*/
+/*	$NetBSD: console.c,v 1.7 2021/05/04 21:09:16 khorben Exp $	*/
 
 /*
  * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
@@ -100,13 +100,24 @@ void print(char *buf)
 	print_ext(WHITE_ON_BLACK, buf);
 }
 
-void print_state(bool ok, char *buf)
+void print_state(state_t state, char *buf)
 {
 	print("[");
-	if (ok)
-		print_ext(GREEN_ON_BLACK, "+");
-	else
-		print_ext(RED_ON_BLACK, "!");
+	switch (state)
+	{
+		case STATE_NORMAL:
+			print_ext(GREEN_ON_BLACK, "+");
+			break;
+		case STATE_ERROR:
+			print_ext(RED_ON_BLACK, "!");
+			break;
+		case STATE_WARNING:
+			print_ext(YELLOW_ON_BLACK, "*");
+			break;
+		default:
+			print_ext(WHITE_ON_BLACK, "?");
+			break;
+	}
 	print("] ");
 	print(buf);
 	print("\n");

Index: src/sys/arch/amd64/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.21 src/sys/arch/amd64/stand/prekern/elf.c:1.22
--- src/sys/arch/amd64/stand/prekern/elf.c:1.21	Thu May  7 17:58:26 2020
+++ src/sys/arch/amd64/stand/prekern/elf.c	Tue May  4 21:09:16 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.21 2020/05/07 17:58:26 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.22 2021/05/04 21:09:16 khorben Exp $	*/
 
 /*
  * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
@@ -426,7 +426,7 @@ elf_kernel_reloc(void)
 	Elf_Sym *sym;
 	size_t i, j;
 
-	print_state(true, "ELF info created");
+	print_state(STATE_NORMAL, "ELF info created");
 
 	/*
 	 * Update all symbol values with the appropriate offset.
@@ -447,7 +447,7 @@ elf_kernel_reloc(void)
 		}
 	}
 
-	print_state(true, "Symbol values updated");
+	print_state(STATE_NORMAL, "Symbol values updated");
 
 	/*
 	 * Perform relocations without addend if there are any.
@@ -482,7 +482,7 @@ elf_kernel_reloc(void)
 		}
 	}
 
-	print_state(true, "REL relocations applied");
+	print_state(STATE_NORMAL, "REL relocations applied");
 
 	/*
 	 * Perform relocations with addend if there are any.
@@ -517,7 +517,7 @@ elf_kernel_reloc(void)
 		}
 	}
 
-	print_state(true, "RELA relocations applied");
+	print_state(STATE_NORMAL, "RELA relocations applied");
 
 	/*
 	 * Get the entry point.
@@ -527,7 +527,7 @@ elf_kernel_reloc(void)
 		fatal("elf_kernel_reloc: entry point not found");
 	}
 
-	print_state(true, "Entry point found");
+	print_state(STATE_NORMAL, "Entry point found");
 
 	return ent;
 }

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.27 src/sys/arch/amd64/stand/prekern/mm.c:1.28
--- src/sys/arch/amd64/stand/prekern/mm.c:1.27	Thu May  7 17:58:26 2020
+++ src/sys/arch/amd64/stand/prekern/mm.c	Tue May  4 21:09:16 2021
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.27 2020/05/07 17:58:26 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.28 2021/05/04 21:09:16 khorben Exp $	*/
 
 /*
  * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
@@ -148,7 +148,7 @@ mm_bootspace_mprotect(void)
 		mm_mprotect(bootspace.segs[i].va, bootspace.segs[i].sz, prot);
 	}
 
-	print_state(true, "Segments protection updated");
+	print_state(STATE_NORMAL, "Segments protection updated");
 }
 
 static size_t
@@ -493,9 +493,9 @@ mm_map_kernel(void)
 {
 	memset(, 0, sizeof(bootspace));
 	mm_map_head();
-	print_state(true, "Head region mapped");
+	print_state(STATE_NORMAL, "Head region mapped");
 	elf_map_sections();
-	print_state(true, "Segments mapped");
+	print_state(STATE_NORMAL, "Segments mapped");
 	mm_map_boot();
-	print_state(true, "Boot region mapped");
+	print_state(STATE_NORMAL, "Boot region mapped");
 }

Index: src/sys/arch/amd64/stand/prekern/prekern.c
diff -u src/sys/arch/amd64/stand/prekern/prekern.c:1.13 src/sys/arch/amd64/stand/prekern/prekern.c:1.14
--- src/sys/arch/amd64/stand/prekern/prekern.c:1.13	Sat May 23 08:25:32 2020
+++ src/sys/arch/amd64/stand/prekern/prekern.c	Tue May  4 

CVS commit: src/sys/arch/amd64/stand/prekern

2020-05-23 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 23 08:25:32 UTC 2020

Modified Files:
src/sys/arch/amd64/stand/prekern: console.c pdir.h prekern.c prekern.h
redef.h trap.S

Log Message:
Bump copyrights.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/console.c \
src/sys/arch/amd64/stand/prekern/trap.S
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/pdir.h
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/amd64/stand/prekern/prekern.c
cvs rdiff -u -r1.22 -r1.23 src/sys/arch/amd64/stand/prekern/prekern.h
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/redef.h

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/stand/prekern/console.c
diff -u src/sys/arch/amd64/stand/prekern/console.c:1.5 src/sys/arch/amd64/stand/prekern/console.c:1.6
--- src/sys/arch/amd64/stand/prekern/console.c:1.5	Sat May 23 08:23:28 2020
+++ src/sys/arch/amd64/stand/prekern/console.c	Sat May 23 08:25:32 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: console.c,v 1.5 2020/05/23 08:23:28 maxv Exp $	*/
+/*	$NetBSD: console.c,v 1.6 2020/05/23 08:25:32 maxv Exp $	*/
 
 /*
- * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
  * by Maxime Villard.
Index: src/sys/arch/amd64/stand/prekern/trap.S
diff -u src/sys/arch/amd64/stand/prekern/trap.S:1.5 src/sys/arch/amd64/stand/prekern/trap.S:1.6
--- src/sys/arch/amd64/stand/prekern/trap.S:1.5	Tue Mar 19 19:15:57 2019
+++ src/sys/arch/amd64/stand/prekern/trap.S	Sat May 23 08:25:32 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: trap.S,v 1.5 2019/03/19 19:15:57 maxv Exp $	*/
+/*	$NetBSD: trap.S,v 1.6 2020/05/23 08:25:32 maxv Exp $	*/
 
 /*
- * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
  * by Maxime Villard.

Index: src/sys/arch/amd64/stand/prekern/pdir.h
diff -u src/sys/arch/amd64/stand/prekern/pdir.h:1.6 src/sys/arch/amd64/stand/prekern/pdir.h:1.7
--- src/sys/arch/amd64/stand/prekern/pdir.h:1.6	Sat Nov  3 08:27:16 2018
+++ src/sys/arch/amd64/stand/prekern/pdir.h	Sat May 23 08:25:32 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: pdir.h,v 1.6 2018/11/03 08:27:16 maxv Exp $	*/
+/*	$NetBSD: pdir.h,v 1.7 2020/05/23 08:25:32 maxv Exp $	*/
 
 /*
- * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
  * by Maxime Villard.

Index: src/sys/arch/amd64/stand/prekern/prekern.c
diff -u src/sys/arch/amd64/stand/prekern/prekern.c:1.12 src/sys/arch/amd64/stand/prekern/prekern.c:1.13
--- src/sys/arch/amd64/stand/prekern/prekern.c:1.12	Sat May 23 08:10:50 2020
+++ src/sys/arch/amd64/stand/prekern/prekern.c	Sat May 23 08:25:32 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: prekern.c,v 1.12 2020/05/23 08:10:50 maxv Exp $	*/
+/*	$NetBSD: prekern.c,v 1.13 2020/05/23 08:25:32 maxv Exp $	*/
 
 /*
- * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
  * by Maxime Villard.

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.22 src/sys/arch/amd64/stand/prekern/prekern.h:1.23
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.22	Thu May  7 21:05:37 2020
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Sat May 23 08:25:32 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: prekern.h,v 1.22 2020/05/07 21:05:37 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.23 2020/05/23 08:25:32 maxv Exp $	*/
 
 /*
- * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
  * by Maxime Villard.

Index: src/sys/arch/amd64/stand/prekern/redef.h
diff -u src/sys/arch/amd64/stand/prekern/redef.h:1.2 src/sys/arch/amd64/stand/prekern/redef.h:1.3
--- src/sys/arch/amd64/stand/prekern/redef.h:1.2	Tue Nov 14 13:58:07 2017
+++ src/sys/arch/amd64/stand/prekern/redef.h	Sat May 23 08:25:32 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: redef.h,v 1.2 2017/11/14 13:58:07 maxv Exp $	*/
+/*	$NetBSD: redef.h,v 1.3 2020/05/23 08:25:32 maxv Exp $	*/
 
 /*
- * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
  * by Maxime Villard.



CVS commit: src/sys/arch/amd64/stand/prekern

2020-05-23 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 23 08:23:29 UTC 2020

Modified Files:
src/sys/arch/amd64/stand/prekern: console.c

Log Message:
Extract putc().


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/console.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/stand/prekern/console.c
diff -u src/sys/arch/amd64/stand/prekern/console.c:1.4 src/sys/arch/amd64/stand/prekern/console.c:1.5
--- src/sys/arch/amd64/stand/prekern/console.c:1.4	Wed Apr  3 19:14:25 2019
+++ src/sys/arch/amd64/stand/prekern/console.c	Sat May 23 08:23:28 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: console.c,v 1.4 2019/04/03 19:14:25 maxv Exp $	*/
+/*	$NetBSD: console.c,v 1.5 2020/05/23 08:23:28 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -64,28 +64,34 @@ static void check_scroll(void)
 	memcpy(cons_start, _buffer[0], CONS_WID * 2 * CONS_HEI);
 }
 
-void print_ext(int color, char *buf)
+static void putc(int color, char c)
 {
 	char *ptr, *scr;
-	size_t i;
 
-	for (i = 0; buf[i] != '\0'; i++) {
-		if (buf[i] == '\n') {
+	if (c == '\n') {
+		cons_x = 0;
+		cons_y++;
+		check_scroll();
+	} else {
+		if (cons_x + 1 == CONS_WID) {
 			cons_x = 0;
 			cons_y++;
 			check_scroll();
-		} else {
-			if (cons_x + 1 == CONS_WID) {
-cons_x = 0;
-cons_y++;
-check_scroll();
-			}
-			ptr = (cons_start + 2 * cons_x + 160 * cons_y);
-			scr = (cons_buffer + 2 * cons_x + 160 * cons_y);
-			ptr[0] = scr[0] = buf[i];
-			ptr[1] = scr[1] = color;
-			cons_x++;
 		}
+		ptr = (cons_start + 2 * cons_x + 160 * cons_y);
+		scr = (cons_buffer + 2 * cons_x + 160 * cons_y);
+		ptr[0] = scr[0] = c;
+		ptr[1] = scr[1] = color;
+		cons_x++;
+	}
+}
+
+void print_ext(int color, char *buf)
+{
+	size_t i;
+
+	for (i = 0; buf[i] != '\0'; i++) {
+		putc(color, buf[i]);
 	}
 }
 



CVS commit: src/sys/arch/amd64/stand/prekern

2020-05-23 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat May 23 08:10:50 UTC 2020

Modified Files:
src/sys/arch/amd64/stand/prekern: prekern.c

Log Message:
Hum, forgot to include this file in my "Clarify." commit on mm.c:rev1.27
and elf.c:rev1.21.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/prekern.c
diff -u src/sys/arch/amd64/stand/prekern/prekern.c:1.11 src/sys/arch/amd64/stand/prekern/prekern.c:1.12
--- src/sys/arch/amd64/stand/prekern/prekern.c:1.11	Tue Mar 19 19:15:57 2019
+++ src/sys/arch/amd64/stand/prekern/prekern.c	Sat May 23 08:10:50 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.c,v 1.11 2019/03/19 19:15:57 maxv Exp $	*/
+/*	$NetBSD: prekern.c,v 1.12 2020/05/23 08:10:50 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -297,6 +297,7 @@ init_prekern(paddr_t pa_start)
 	 * Relocate the kernel.
 	 */
 	mm_map_kernel();
+	elf_build_info();
 	ent = elf_kernel_reloc();
 	mm_bootspace_mprotect();
 



CVS commit: src/sys/arch/amd64/stand/prekern

2020-05-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May 21 08:20:25 UTC 2020

Modified Files:
src/sys/arch/amd64/stand/prekern: prng.c

Log Message:
Mmh, should check cpuid_level first.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/prng.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/stand/prekern/prng.c
diff -u src/sys/arch/amd64/stand/prekern/prng.c:1.2 src/sys/arch/amd64/stand/prekern/prng.c:1.3
--- src/sys/arch/amd64/stand/prekern/prng.c:1.2	Sun Nov 26 11:08:34 2017
+++ src/sys/arch/amd64/stand/prekern/prng.c	Thu May 21 08:20:25 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: prng.c,v 1.2 2017/11/26 11:08:34 maxv Exp $	*/
+/*	$NetBSD: prng.c,v 1.3 2020/05/21 08:20:25 maxv Exp $	*/
 
 /*
- * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
  * by Maxime Villard.
@@ -148,6 +148,7 @@ prng_get_entropy_data(SHA512_CTX *ctx)
 void
 prng_init(void)
 {
+	extern int cpuid_level;
 	uint8_t digest[SHA512_DIGEST_LENGTH];
 	SHA512_CTX ctx;
 	u_int descs[4];
@@ -155,10 +156,14 @@ prng_init(void)
 	memset(, 0, sizeof(rng));
 
 	/* detect cpu features */
-	cpuid(0x07, 0x00, descs);
-	has_rdseed = (descs[1] & CPUID_SEF_RDSEED) != 0;
-	cpuid(0x01, 0x00, descs);
-	has_rdrand = (descs[2] & CPUID2_RDRAND) != 0;
+	if (cpuid_level >= 0x07) {
+		cpuid(0x07, 0x00, descs);
+		has_rdseed = (descs[1] & CPUID_SEF_RDSEED) != 0;
+	}
+	if (cpuid_level >= 0x01) {
+		cpuid(0x01, 0x00, descs);
+		has_rdrand = (descs[2] & CPUID2_RDRAND) != 0;
+	}
 
 	SHA512_Init();
 	prng_get_entropy_file();



CVS commit: src/sys/arch/amd64/stand/prekern

2020-05-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May  7 21:05:37 UTC 2020

Modified Files:
src/sys/arch/amd64/stand/prekern: prekern.h

Log Message:
Forgot to commit this file as part of elf.c::rev1.21 mm.c::rev1.27.


To generate a diff of this commit:
cvs rdiff -u -r1.21 -r1.22 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.21 src/sys/arch/amd64/stand/prekern/prekern.h:1.22
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.21	Tue May  5 19:26:47 2020
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Thu May  7 21:05:37 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.21 2020/05/05 19:26:47 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.22 2020/05/07 21:05:37 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -89,8 +89,9 @@ void print_banner(void);
 /* elf.c */
 size_t elf_get_head_size(vaddr_t);
 void elf_build_head(vaddr_t);
+void elf_fixup_boot(vaddr_t, paddr_t);
 void elf_map_sections(void);
-void elf_build_boot(vaddr_t, paddr_t);
+void elf_build_info(void);
 vaddr_t elf_kernel_reloc(void);
 
 /* locore.S */



CVS commit: src/sys/arch/amd64/stand/prekern

2020-05-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May  7 17:58:26 UTC 2020

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c

Log Message:
Clarify.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.26 -r1.27 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.20 src/sys/arch/amd64/stand/prekern/elf.c:1.21
--- src/sys/arch/amd64/stand/prekern/elf.c:1.20	Thu May  7 16:49:59 2020
+++ src/sys/arch/amd64/stand/prekern/elf.c	Thu May  7 17:58:26 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.20 2020/05/07 16:49:59 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.21 2020/05/07 17:58:26 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
@@ -300,6 +300,37 @@ elf_build_head(vaddr_t headva)
 }
 
 void
+elf_fixup_boot(vaddr_t bootva, paddr_t bootpa)
+{
+	const paddr_t basepa = kernpa_start;
+	const vaddr_t headva = (vaddr_t)eif.ehdr;
+	size_t i, offboot;
+
+	/*
+	 * Fix up the 'sh_offset' field of the REL/RELA/SYM/STR sections, which
+	 * are all in the "boot" region.
+	 */
+	for (i = 0; i < eif.ehdr->e_shnum; i++) {
+		if (eif.shdr[i].sh_type != SHT_STRTAB &&
+		eif.shdr[i].sh_type != SHT_REL &&
+		eif.shdr[i].sh_type != SHT_RELA &&
+		eif.shdr[i].sh_type != SHT_SYMTAB) {
+			continue;
+		}
+		if (eif.shdr[i].sh_offset == 0) {
+			/* The bootloader dropped it. */
+			continue;
+		}
+
+		/* Offset of the section within the boot region. */
+		offboot = basepa + eif.shdr[i].sh_offset - bootpa;
+
+		/* We want (headva + sh_offset) to be the VA of the region. */
+		eif.shdr[i].sh_offset = (bootva + offboot - headva);
+	}
+}
+
+void
 elf_map_sections(void)
 {
 	const paddr_t basepa = kernpa_start;
@@ -333,45 +364,27 @@ elf_map_sections(void)
 
 		secva = mm_map_segment(segtype, secpa, secsz, secalign);
 
-		/* We want (headva + sh_offset) to be the VA of the section. */
+		/*
+		 * Fix up the 'sh_offset' field of the NOBITS/PROGBITS sections.
+		 * We want (headva + sh_offset) to be the VA of the section.
+		 */
 		ASSERT(secva > headva);
 		shdr->sh_offset = secva - headva;
 	}
 }
 
 void
-elf_build_boot(vaddr_t bootva, paddr_t bootpa)
+elf_build_info(void)
 {
-	const paddr_t basepa = kernpa_start;
-	const vaddr_t headva = (vaddr_t)eif.ehdr;
-	size_t i, j, offboot;
-
-	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (eif.shdr[i].sh_type != SHT_STRTAB &&
-		eif.shdr[i].sh_type != SHT_REL &&
-		eif.shdr[i].sh_type != SHT_RELA &&
-		eif.shdr[i].sh_type != SHT_SYMTAB) {
-			continue;
-		}
-		if (eif.shdr[i].sh_offset == 0) {
-			/* hasn't been loaded */
-			continue;
-		}
-
-		/* Offset of the section within the boot region. */
-		offboot = basepa + eif.shdr[i].sh_offset - bootpa;
-
-		/* We want (headva + sh_offset) to be the VA of the region. */
-		eif.shdr[i].sh_offset = (bootva + offboot - headva);
-	}
+	size_t i, j;
 
 	/* Locate the section names */
 	j = eif.ehdr->e_shstrndx;
 	if (j == SHN_UNDEF) {
-		fatal("elf_build_boot: shstrtab not found");
+		fatal("elf_build_info: shstrtab not found");
 	}
 	if (j >= eif.ehdr->e_shnum) {
-		fatal("elf_build_boot: wrong shstrtab index");
+		fatal("elf_build_info: wrong shstrtab index");
 	}
 	eif.shstrtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
 	eif.shstrsz = eif.shdr[j].sh_size;
@@ -382,10 +395,10 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 			break;
 	}
 	if (i == eif.ehdr->e_shnum) {
-		fatal("elf_build_boot: symtab not found");
+		fatal("elf_build_info: symtab not found");
 	}
 	if (eif.shdr[i].sh_offset == 0) {
-		fatal("elf_build_boot: symtab not loaded");
+		fatal("elf_build_info: symtab not loaded");
 	}
 	eif.symtab = (Elf_Sym *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
 	eif.symcnt = eif.shdr[i].sh_size / sizeof(Elf_Sym);
@@ -393,13 +406,13 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 	/* Also locate the string table */
 	j = eif.shdr[i].sh_link;
 	if (j == SHN_UNDEF || j >= eif.ehdr->e_shnum) {
-		fatal("elf_build_boot: wrong strtab index");
+		fatal("elf_build_info: wrong strtab index");
 	}
 	if (eif.shdr[j].sh_type != SHT_STRTAB) {
-		fatal("elf_build_boot: wrong strtab type");
+		fatal("elf_build_info: wrong strtab type");
 	}
 	if (eif.shdr[j].sh_offset == 0) {
-		fatal("elf_build_boot: strtab not loaded");
+		fatal("elf_build_info: strtab not loaded");
 	}
 	eif.strtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
 	eif.strsz = eif.shdr[j].sh_size;

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.26 src/sys/arch/amd64/stand/prekern/mm.c:1.27
--- src/sys/arch/amd64/stand/prekern/mm.c:1.26	Thu May  7 17:10:02 2020
+++ src/sys/arch/amd64/stand/prekern/mm.c	Thu May  7 17:58:26 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.26 

CVS commit: src/sys/arch/amd64/stand/prekern

2020-05-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May  7 17:10:02 UTC 2020

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Explain more.


To generate a diff of this commit:
cvs rdiff -u -r1.25 -r1.26 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.25 src/sys/arch/amd64/stand/prekern/mm.c:1.26
--- src/sys/arch/amd64/stand/prekern/mm.c:1.25	Sat Feb 15 10:41:25 2020
+++ src/sys/arch/amd64/stand/prekern/mm.c	Thu May  7 17:10:02 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.25 2020/02/15 10:41:25 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.26 2020/05/07 17:10:02 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
@@ -248,7 +248,7 @@ mm_randva_kregion(size_t size, size_t pa
 }
 
 static paddr_t
-bootspace_getend(void)
+bootspace_get_kern_segs_end_pa(void)
 {
 	paddr_t pa, max = 0;
 	size_t i;
@@ -410,21 +410,29 @@ mm_map_boot(void)
 	 * the number of pages entered is lower.
 	 */
 
-	/* Create the page tree */
+	/* Create the page tree, starting at a random VA */
 	size = (NKL2_KIMG_ENTRIES + 1) * NBPD_L2;
 	randva = mm_randva_kregion(size, PAGE_SIZE);
 
-	/* Enter the area and build the ELF info */
-	bootpa = bootspace_getend();
+	/* The "boot" region begins right after the kernel segments */
+	bootpa = bootspace_get_kern_segs_end_pa();
+
+	/* The prekern consumed some memory up until pa_avail, this covers
+	 * SYM+REL and EXTRA */
 	size = (pa_avail - bootpa);
 	npages = size / PAGE_SIZE;
+
+	/* Enter the whole area linearly */
 	for (i = 0; i < npages; i++) {
 		mm_enter_pa(bootpa + i * PAGE_SIZE,
 		randva + i * PAGE_SIZE, MM_PROT_READ|MM_PROT_WRITE);
 	}
+
+	/* At this point both "head" and "boot" are mapped, so we can build
+	 * the ELF info */
 	elf_build_boot(randva, bootpa);
 
-	/* Enter the ISA I/O MEM */
+	/* Map the ISA I/O MEM right after EXTRA */
 	iom_base = randva + npages * PAGE_SIZE;
 	npages = IOM_SIZE / PAGE_SIZE;
 	for (i = 0; i < npages; i++) {
@@ -451,23 +459,25 @@ mm_map_boot(void)
  * ++-+---+--+---+
  * | ELF HEADER | SECTION HEADERS | KERN SECTIONS | SYM+REL SECTIONS | EXTRA |
  * ++-+---+--+---+
- * Which we abstract into several "regions":
+ * This was done in the loadfile_elf32.c:loadfile_dynamic() function.
+ *
+ * We abstract this layout into several "regions":
  * +--+---+--+
- * | Head region  | Several segs  |   Boot region|
+ * | Head region  |  Kernel segs  |   Boot region|
  * +--+---+--+
- * See loadfile_elf32.c:loadfile_dynamic() for the details.
  *
  * There is a variable number of independent regions we create: one head,
  * several kernel segments, one boot. They are all mapped at random VAs.
  *
- * Head contains the ELF Header and ELF Section Headers, and we use them to
- * map the rest of the regions. Head must be placed in both virtual memory
- * and physical memory *before* the rest.
+ * "Head" contains the ELF Header and ELF Section Headers, and we use them to
+ * map the rest of the regions. Head must be placed *before* the other
+ * regions, in both virtual memory and physical memory.
  *
- * The Kernel Sections are mapped at random VAs using individual segments
- * in bootspace.
+ * The "Kernel Segments" contain the kernel SHT_NOBITS and SHT_PROGBITS
+ * sections, in a 1:1 manner (one segment is associated with one section).
+ * The segments are mapped at random VAs and referenced in bootspace.segs[].
  *
- * Boot contains various information, including the ELF Sym+Rel sections,
+ * "Boot" contains various information, including the ELF Sym+Rel sections,
  * plus extra memory the prekern has used so far; it is a region that the
  * kernel will eventually use for module_map. Boot is placed *after* the
  * other regions in physical memory. In virtual memory however there is no



CVS commit: src/sys/arch/amd64/stand/prekern

2020-05-07 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu May  7 16:49:59 UTC 2020

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c

Log Message:
If we encounter relocations from a section that the bootloader dropped,
AND if the section is a note, then skip the relocations.

Considering a note that the bootloader dropped, there are two possible
sides for the relocations: (1) the relocations from the note towards the
rest of the binary, and (2) the relocations from the rest of the binary
towards the note.

We skip (1), which is correct, because the notes do not play any role at
run time. If we encounter (2) however then there is a bug in the kernel,
so add a sanity check against that.

This fixes KASLR since the latest Xen changes (which introduced .note.Xen).


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/amd64/stand/prekern/elf.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.19 src/sys/arch/amd64/stand/prekern/elf.c:1.20
--- src/sys/arch/amd64/stand/prekern/elf.c:1.19	Tue May  5 19:26:47 2020
+++ src/sys/arch/amd64/stand/prekern/elf.c	Thu May  7 16:49:59 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: elf.c,v 1.19 2020/05/05 19:26:47 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.20 2020/05/07 16:49:59 maxv Exp $	*/
 
 /*
- * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
  * by Maxime Villard.
@@ -60,6 +60,40 @@ elf_check_header(void)
 	return 0;
 }
 
+static bool
+elf_section_mappable(Elf_Shdr *shdr)
+{
+	if (!(shdr->sh_flags & SHF_ALLOC)) {
+		return false;
+	}
+	if (shdr->sh_type != SHT_NOBITS &&
+	shdr->sh_type != SHT_PROGBITS) {
+		return false;
+	}
+	return true;
+}
+
+static bool
+elf_can_drop_unmappable(Elf_Shdr *shdr)
+{
+	/*
+	 * We found relocations from the section 'shdr' towards the rest of
+	 * the binary, but 'shdr' is not mapped. Decide whether to skip the
+	 * relocations from this section.
+	 *
+	 * We skip only if it is a note. It means that we allow notes to
+	 * have relocations towards the rest of the binary, typically with
+	 * the ".note.Xen" section. Notes do not play any role at run time.
+	 *
+	 * Any section other than a note is the sign there is a design
+	 * mistake in the kernel (variables stored outside of rodata/data).
+	 */
+	if (shdr->sh_type == SHT_NOTE) {
+		return true;
+	}
+	return false;
+}
+
 static vaddr_t
 elf_get_entrypoint(void)
 {
@@ -144,6 +178,12 @@ elf_sym_lookup(size_t symidx)
 
 		fatal("elf_sym_lookup: external symbol");
 	}
+	if (sym->st_shndx >= eif.ehdr->e_shnum) {
+		fatal("elf_sym_lookup: st_shndx is malformed");
+	}
+	if (!elf_section_mappable([sym->st_shndx])) {
+		fatal("elf_sym_lookup: st_shndx not mappable");
+	}
 	if (sym->st_value == 0) {
 		fatal("elf_sym_lookup: zero value");
 	}
@@ -259,19 +299,6 @@ elf_build_head(vaddr_t headva)
 	}
 }
 
-static bool
-elf_section_mappable(Elf_Shdr *shdr)
-{
-	if (!(shdr->sh_flags & SHF_ALLOC)) {
-		return false;
-	}
-	if (shdr->sh_type != SHT_NOBITS &&
-	shdr->sh_type != SHT_PROGBITS) {
-		return false;
-	}
-	return true;
-}
-
 void
 elf_map_sections(void)
 {
@@ -429,6 +456,9 @@ elf_kernel_reloc(void)
 			fatal("elf_kernel_reloc: REL sh_info is malformed");
 		}
 		if (!elf_section_mappable([secidx])) {
+			if (elf_can_drop_unmappable([secidx])) {
+continue;
+			}
 			fatal("elf_kernel_reloc: REL sh_info not mappable");
 		}
 		base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
@@ -461,6 +491,9 @@ elf_kernel_reloc(void)
 			fatal("elf_kernel_reloc: RELA sh_info is malformed");
 		}
 		if (!elf_section_mappable([secidx])) {
+			if (elf_can_drop_unmappable([secidx])) {
+continue;
+			}
 			fatal("elf_kernel_reloc: RELA sh_info not mappable");
 		}
 		base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;



CVS commit: src/sys/arch/amd64/stand/prekern

2020-05-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue May  5 19:26:47 UTC 2020

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c prekern.h

Log Message:
Gather the section filtering in a single function, and add a sanity check
when relocating, to make sure the section we're accessing is mappable.

Currently this check fails, because of the Xen section, which has RELAs but
is an unmappable unallocated note.

Also improve the prekern ASSERTs while here.


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.18 src/sys/arch/amd64/stand/prekern/elf.c:1.19
--- src/sys/arch/amd64/stand/prekern/elf.c:1.18	Sat Jan  5 22:11:07 2019
+++ src/sys/arch/amd64/stand/prekern/elf.c	Tue May  5 19:26:47 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.18 2019/01/05 22:11:07 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.19 2020/05/05 19:26:47 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -259,6 +259,19 @@ elf_build_head(vaddr_t headva)
 	}
 }
 
+static bool
+elf_section_mappable(Elf_Shdr *shdr)
+{
+	if (!(shdr->sh_flags & SHF_ALLOC)) {
+		return false;
+	}
+	if (shdr->sh_type != SHT_NOBITS &&
+	shdr->sh_type != SHT_PROGBITS) {
+		return false;
+	}
+	return true;
+}
+
 void
 elf_map_sections(void)
 {
@@ -273,11 +286,7 @@ elf_map_sections(void)
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
 		shdr = [i];
 
-		if (!(shdr->sh_flags & SHF_ALLOC)) {
-			continue;
-		}
-		if (shdr->sh_type != SHT_NOBITS &&
-		shdr->sh_type != SHT_PROGBITS) {
+		if (!elf_section_mappable(shdr)) {
 			continue;
 		}
 
@@ -383,10 +392,10 @@ elf_kernel_reloc(void)
 	 * Update all symbol values with the appropriate offset.
 	 */
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (eif.shdr[i].sh_type != SHT_NOBITS &&
-		eif.shdr[i].sh_type != SHT_PROGBITS) {
+		if (!elf_section_mappable([i])) {
 			continue;
 		}
+
 		ASSERT(eif.shdr[i].sh_offset != 0);
 		secva = baseva + eif.shdr[i].sh_offset;
 		for (j = 0; j < eif.symcnt; j++) {
@@ -417,7 +426,10 @@ elf_kernel_reloc(void)
 
 		secidx = eif.shdr[i].sh_info;
 		if (secidx >= eif.ehdr->e_shnum) {
-			fatal("elf_kernel_reloc: wrong REL relocation");
+			fatal("elf_kernel_reloc: REL sh_info is malformed");
+		}
+		if (!elf_section_mappable([secidx])) {
+			fatal("elf_kernel_reloc: REL sh_info not mappable");
 		}
 		base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
 
@@ -446,7 +458,10 @@ elf_kernel_reloc(void)
 
 		secidx = eif.shdr[i].sh_info;
 		if (secidx >= eif.ehdr->e_shnum) {
-			fatal("elf_kernel_reloc: wrong RELA relocation");
+			fatal("elf_kernel_reloc: RELA sh_info is malformed");
+		}
+		if (!elf_section_mappable([secidx])) {
+			fatal("elf_kernel_reloc: RELA sh_info not mappable");
 		}
 		base = (uintptr_t)eif.ehdr + eif.shdr[secidx].sh_offset;
 

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.20 src/sys/arch/amd64/stand/prekern/prekern.h:1.21
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.20	Wed Jun 20 11:49:37 2018
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Tue May  5 19:26:47 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.20 2018/06/20 11:49:37 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.21 2020/05/05 19:26:47 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -37,7 +37,7 @@
 #include "pdir.h"
 #include "redef.h"
 
-#define ASSERT(a) if (!(a)) fatal("ASSERT");
+#define ASSERT(a) if (!(a)) fatal("ASSERT: " #a);
 typedef uint64_t pte_prot_t;
 #define WHITE_ON_BLACK 0x07
 #define RED_ON_BLACK 0x04



CVS commit: src/sys/arch/amd64/stand/prekern

2020-02-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Feb 15 10:41:25 UTC 2020

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Explain more.


To generate a diff of this commit:
cvs rdiff -u -r1.24 -r1.25 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.24 src/sys/arch/amd64/stand/prekern/mm.c:1.25
--- src/sys/arch/amd64/stand/prekern/mm.c:1.24	Sat Mar  9 08:42:25 2019
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sat Feb 15 10:41:25 2020
@@ -1,7 +1,7 @@
-/*	$NetBSD: mm.c,v 1.24 2019/03/09 08:42:25 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.25 2020/02/15 10:41:25 maxv Exp $	*/
 
 /*
- * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
+ * Copyright (c) 2017-2020 The NetBSD Foundation, Inc. All rights reserved.
  *
  * This code is derived from software contributed to The NetBSD Foundation
  * by Maxime Villard.
@@ -289,6 +289,12 @@ mm_shift_segment(vaddr_t va, size_t page
 	size_t shiftsize, offset;
 	uint64_t rnd;
 
+	/*
+	 * If possible, shift the segment in memory using a random offset. Once
+	 * shifted the segment remains in the same page, of size pagesz. Make
+	 * sure to respect the ELF alignment constraint.
+	 */
+
 	if (elfalign == 0) {
 		elfalign = ELFROUND;
 	}
@@ -317,6 +323,13 @@ mm_map_head(void)
 	vaddr_t randva;
 
 	/*
+	 * The HEAD window is 1GB below the main KASLR window. This is to
+	 * ensure that head always comes first in virtual memory. The reason
+	 * for that is that we use (headva + sh_offset), and sh_offset is
+	 * unsigned.
+	 */
+
+	/*
 	 * To get the size of the head, we give a look at the read-only
 	 * mapping of the kernel we created in locore. We're identity mapped,
 	 * so kernpa = kernva.
@@ -324,6 +337,10 @@ mm_map_head(void)
 	size = elf_get_head_size((vaddr_t)kernpa_start);
 	npages = size / PAGE_SIZE;
 
+	/*
+	 * Choose a random range of VAs in the HEAD window, and create the page
+	 * tree for it.
+	 */
 	prng_get_rand(, sizeof(rnd));
 	randva = rounddown(HEAD_WINDOW_BASE + rnd % (HEAD_WINDOW_SIZE - size),
 	PAGE_SIZE);
@@ -355,22 +372,27 @@ mm_map_segment(int segtype, paddr_t pa, 
 		pagesz = NBPD_L2;
 	}
 
+	/* Create the page tree */
 	size = roundup(elfsz, pagesz);
 	randva = mm_randva_kregion(size, pagesz);
 
+	/* Enter the segment */
 	npages = size / PAGE_SIZE;
 	for (i = 0; i < npages; i++) {
 		mm_enter_pa(pa + i * PAGE_SIZE,
 		randva + i * PAGE_SIZE, MM_PROT_READ|MM_PROT_WRITE);
 	}
 
+	/* Shift the segment in memory */
 	offset = mm_shift_segment(randva, pagesz, elfsz, elfalign);
 	ASSERT(offset + elfsz <= size);
 
+	/* Fill the paddings */
 	pad = pads[segtype];
 	memset((void *)randva, pad, offset);
 	memset((void *)(randva + offset + elfsz), pad, size - elfsz - offset);
 
+	/* Register the bootspace information */
 	bootspace_addseg(segtype, randva, pa, size);
 
 	return (randva + offset);
@@ -425,12 +447,31 @@ mm_map_boot(void)
 }
 
 /*
- * There is a variable number of independent regions: one head, several kernel
- * segments, one boot. They are all mapped at random VAs.
+ * The bootloader has set up the following layout of physical memory:
+ * ++-+---+--+---+
+ * | ELF HEADER | SECTION HEADERS | KERN SECTIONS | SYM+REL SECTIONS | EXTRA |
+ * ++-+---+--+---+
+ * Which we abstract into several "regions":
+ * +--+---+--+
+ * | Head region  | Several segs  |   Boot region|
+ * +--+---+--+
+ * See loadfile_elf32.c:loadfile_dynamic() for the details.
+ *
+ * There is a variable number of independent regions we create: one head,
+ * several kernel segments, one boot. They are all mapped at random VAs.
  *
  * Head contains the ELF Header and ELF Section Headers, and we use them to
- * map the rest of the regions. Head must be placed in memory *before* the
- * other regions.
+ * map the rest of the regions. Head must be placed in both virtual memory
+ * and physical memory *before* the rest.
+ *
+ * The Kernel Sections are mapped at random VAs using individual segments
+ * in bootspace.
+ *
+ * Boot contains various information, including the ELF Sym+Rel sections,
+ * plus extra memory the prekern has used so far; it is a region that the
+ * kernel will eventually use for module_map. Boot is placed *after* the
+ * other regions in physical memory. In virtual memory however there is no
+ * constraint, so its VA is randomly selected in the main KASLR window.
  *
  * At the end of this function, the bootspace structure is fully constructed.
  */



CVS commit: src/sys/arch/amd64/stand/prekern

2019-04-03 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Apr  3 19:14:25 UTC 2019

Modified Files:
src/sys/arch/amd64/stand/prekern: console.c

Log Message:
When scrolling the screen don't forget to update the last line. Whatever,
there is no case where the screen scrolls actually.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/stand/prekern/console.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/stand/prekern/console.c
diff -u src/sys/arch/amd64/stand/prekern/console.c:1.3 src/sys/arch/amd64/stand/prekern/console.c:1.4
--- src/sys/arch/amd64/stand/prekern/console.c:1.3	Fri Nov 17 07:07:52 2017
+++ src/sys/arch/amd64/stand/prekern/console.c	Wed Apr  3 19:14:25 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: console.c,v 1.3 2017/11/17 07:07:52 maxv Exp $	*/
+/*	$NetBSD: console.c,v 1.4 2019/04/03 19:14:25 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -61,7 +61,7 @@ static void check_scroll(void)
 	memset(_buffer[0] + (CONS_WID * 2) * (CONS_HEI-1), 0,
 	(CONS_WID * 2));
 	cons_y--;
-	memcpy(cons_start, _buffer[0], (CONS_WID * 2) * (CONS_HEI-1));
+	memcpy(cons_start, _buffer[0], CONS_WID * 2 * CONS_HEI);
 }
 
 void print_ext(int color, char *buf)



CVS commit: src/sys/arch/amd64/stand/prekern

2019-03-19 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Mar 19 19:15:57 UTC 2019

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S prekern.c trap.S

Log Message:
Fix/remove some half-baked stuff I left in the prekern:

 - Page-align the idt store, to be extra sure.
 - Remove unneeded prototypes.
 - Drop the TSS, we don't care and aren't even using it.
 - Initialize %ss with a default value.
 - Fix three exception handlers, no need to push an error code.

No actual impact, because these things are used only when returning from
exceptions received in the prekern; these exceptions are not supposed to
be ever received, never are, and if they were we wouldn't return anyway.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/amd64/stand/prekern/locore.S \
src/sys/arch/amd64/stand/prekern/prekern.c
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/trap.S

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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.10 src/sys/arch/amd64/stand/prekern/locore.S:1.11
--- src/sys/arch/amd64/stand/prekern/locore.S:1.10	Sat Mar  9 08:42:25 2019
+++ src/sys/arch/amd64/stand/prekern/locore.S	Tue Mar 19 19:15:57 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.10 2019/03/09 08:42:25 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.11 2019/03/19 19:15:57 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -193,8 +193,6 @@ LABEL(gdt64_start)
 	.quad 0x	/* always empty */
 	.quad 0x00af9a00	/* kernel CS */
 	.quad 0x00cf9200	/* kernel DS */
-	.quad 0x	/* kernel TSS [1/2] */
-	.quad 0x	/* kernel TSS [2/2] */
 END(gdt64_start)
 gdt64_end:
 
@@ -569,6 +567,9 @@ longmode:
 	movw	%ax,%gs
 	movw	%ax,%fs
 
+	movw	$GSEL(GDATA_SEL, SEL_KPL),%ax
+	movw	%ax,%ss
+
 	/* The first physical page available. */
 	leaq	(TABLESIZE)(%rsi),%rdi
 
Index: src/sys/arch/amd64/stand/prekern/prekern.c
diff -u src/sys/arch/amd64/stand/prekern/prekern.c:1.10 src/sys/arch/amd64/stand/prekern/prekern.c:1.11
--- src/sys/arch/amd64/stand/prekern/prekern.c:1.10	Sun Aug 12 12:42:54 2018
+++ src/sys/arch/amd64/stand/prekern/prekern.c	Tue Mar 19 19:15:57 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.c,v 1.10 2018/08/12 12:42:54 maxv Exp $	*/
+/*	$NetBSD: prekern.c,v 1.11 2019/03/19 19:15:57 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -46,15 +46,7 @@ struct bootinfo bootinfo;
 
 extern paddr_t kernpa_start, kernpa_end;
 
-static uint8_t idtstore[PAGE_SIZE];
-static uint8_t faultstack[PAGE_SIZE];
-static struct x86_64_tss prekern_tss;
-
-/* GDT offsets */
-#define PREKERN_GDT_NUL_OFF	(0 * 8)
-#define PREKERN_GDT_CS_OFF	(1 * 8)
-#define PREKERN_GDT_DS_OFF	(2 * 8)
-#define PREKERN_GDT_TSS_OFF	(3 * 8)
+static uint8_t idtstore[PAGE_SIZE] __aligned(PAGE_SIZE);
 
 #define IDTVEC(name) __CONCAT(X, name)
 typedef void (vector)(void);
@@ -83,14 +75,6 @@ struct smallframe {
 	uint64_t sf_ss;
 };
 
-static void setregion(struct region_descriptor *, void *, uint16_t);
-static void setgate(struct gate_descriptor *, void *, int, int, int, int);
-static void set_sys_segment(struct sys_segment_descriptor *, void *,
-size_t, int, int, int);
-static void set_sys_gdt(int, void *, size_t, int, int, int);
-static void init_tss(void);
-static void init_idt(void);
-
 void trap(struct smallframe *);
 
 static char *trap_type[] = {
@@ -142,6 +126,8 @@ trap(struct smallframe *sf)
 	while (1);
 }
 
+/* -- */
+
 static void
 setregion(struct region_descriptor *rd, void *base, uint16_t limit)
 {
@@ -167,42 +153,6 @@ setgate(struct gate_descriptor *gd, void
 }
 
 static void
-set_sys_segment(struct sys_segment_descriptor *sd, void *base, size_t limit,
-int type, int dpl, int gran)
-{
-	memset(sd, 0, sizeof(*sd));
-	sd->sd_lolimit = (unsigned)limit;
-	sd->sd_lobase = (uint64_t)base;
-	sd->sd_type = type;
-	sd->sd_dpl = dpl;
-	sd->sd_p = 1;
-	sd->sd_hilimit = (unsigned)limit >> 16;
-	sd->sd_gran = gran;
-	sd->sd_hibase = (uint64_t)base >> 24;
-}
-
-static void
-set_sys_gdt(int slotoff, void *base, size_t limit, int type, int dpl, int gran)
-{
-	struct sys_segment_descriptor sd;
-	extern uint64_t *gdt64_start;
-
-	set_sys_segment(, base, limit, type, dpl, gran);
-
-	memcpy(_start + slotoff, , sizeof(sd));
-}
-
-static void
-init_tss(void)
-{
-	memset(_tss, 0, sizeof(prekern_tss));
-	prekern_tss.tss_ist[0] = (uintptr_t)([PAGE_SIZE-1]) & ~0xf;
-
-	set_sys_gdt(PREKERN_GDT_TSS_OFF, _tss,
-	sizeof(struct x86_64_tss) - 1, SDT_SYS386TSS, SEL_KPL, 0);
-}
-
-static void
 init_idt(void)
 {
 	struct region_descriptor region;
@@ -331,10 +281,9 @@ init_prekern(paddr_t pa_start)
 	mm_init(pa_start);
 
 	/*
-	 * Init the TSS and IDT. We mostly don't 

CVS commit: src/sys/arch/amd64/stand/prekern

2019-01-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Jan  5 22:11:07 UTC 2019

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c

Log Message:
Apply amd64/kobj_machdep.c::rev1.7 to the prekern too, to fix the
relocation with updated binutils.


To generate a diff of this commit:
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/amd64/stand/prekern/elf.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.17 src/sys/arch/amd64/stand/prekern/elf.c:1.18
--- src/sys/arch/amd64/stand/prekern/elf.c:1.17	Tue Nov 21 07:56:05 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Sat Jan  5 22:11:07 2019
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.17 2017/11/21 07:56:05 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.18 2019/01/05 22:11:07 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -196,6 +196,7 @@ elf_apply_reloc(uintptr_t relocbase, con
 		break;
 
 	case R_X86_64_PC32:	/* S + A - P */
+	case R_X86_64_PLT32:
 		addr = elf_sym_lookup(symidx);
 		where32 = (Elf32_Addr *)where;
 		val32 = (Elf32_Addr)(addr + addend - (Elf64_Addr)where);



CVS commit: src/sys/arch/amd64/stand/prekern

2018-11-03 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Nov  3 08:27:16 UTC 2018

Modified Files:
src/sys/arch/amd64/stand/prekern: pdir.h

Log Message:
Remove VA_SIGN_POS from the computation of the indexes, it is not needed.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/pdir.h

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/stand/prekern/pdir.h
diff -u src/sys/arch/amd64/stand/prekern/pdir.h:1.5 src/sys/arch/amd64/stand/prekern/pdir.h:1.6
--- src/sys/arch/amd64/stand/prekern/pdir.h:1.5	Sun Aug 12 12:42:54 2018
+++ src/sys/arch/amd64/stand/prekern/pdir.h	Sat Nov  3 08:27:16 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: pdir.h,v 1.5 2018/08/12 12:42:54 maxv Exp $	*/
+/*	$NetBSD: pdir.h,v 1.6 2018/11/03 08:27:16 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -48,9 +48,6 @@
 #define NKL3_KIMG_ENTRIES	1
 #define NKL2_KIMG_ENTRIES	48
 
-/*
- * Now we define various constants for playing with virtual addresses.
- */
 #define L1_SHIFT	12
 #define L2_SHIFT	21
 #define L3_SHIFT	30
@@ -70,21 +67,10 @@
 #define L2_FRAME	(L3_FRAME|L2_MASK)
 #define L1_FRAME	(L2_FRAME|L1_MASK)
 
-/*
- * Mask to get rid of the sign-extended part of addresses.
- */
 #define VA_SIGN_MASK		0x
 #define VA_SIGN_NEG(va)		((va) | VA_SIGN_MASK)
-/* XXXfvdl this one's not right */
-#define VA_SIGN_POS(va)		((va) & ~VA_SIGN_MASK)
-
-/*
- * pl*_i: generate index into pde/pte arrays in virtual space
- *
- * pl_i(va, X) == plX_i(va) <= pl_i_roundup(va, X)
- */
-#define pl1_i(VA)	(((VA_SIGN_POS(VA)) & L1_FRAME) >> L1_SHIFT)
-#define pl2_i(VA)	(((VA_SIGN_POS(VA)) & L2_FRAME) >> L2_SHIFT)
-#define pl3_i(VA)	(((VA_SIGN_POS(VA)) & L3_FRAME) >> L3_SHIFT)
-#define pl4_i(VA)	(((VA_SIGN_POS(VA)) & L4_FRAME) >> L4_SHIFT)
 
+#define pl1_i(va)	(((va) & L1_FRAME) >> L1_SHIFT)
+#define pl2_i(va)	(((va) & L2_FRAME) >> L2_SHIFT)
+#define pl3_i(va)	(((va) & L3_FRAME) >> L3_SHIFT)
+#define pl4_i(va)	(((va) & L4_FRAME) >> L4_SHIFT)



CVS commit: src/sys/arch/amd64/stand/prekern

2018-06-02 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Sat Jun  2 14:31:40 UTC 2018

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile

Log Message:
Disable MKSANITIZER


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/Makefile

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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.6 src/sys/arch/amd64/stand/prekern/Makefile:1.7
--- src/sys/arch/amd64/stand/prekern/Makefile:1.6	Sat Dec 23 01:48:30 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Sat Jun  2 10:31:40 2018
@@ -1,10 +1,11 @@
-#	$NetBSD: Makefile,v 1.6 2017/12/23 06:48:30 ryoon Exp $
+#	$NetBSD: Makefile,v 1.7 2018/06/02 14:31:40 christos Exp $
 
 PROG=		prekern
 SRCS=		locore.S trap.S prekern.c mm.c console.c elf.c prng.c
 
 NOSSP=		# defined
 NOPIE=		# defined
+NOSANITIZER=	# defined
 NOMAN=		1
 
 S=	${.CURDIR}/../../../..
@@ -14,7 +15,7 @@ S=	${.CURDIR}/../../../..
 BINDIR=		/usr/mdec
 BINMODE=	444
 
-.include 
+.include 
 
 CPPFLAGS+=	-I. -I${S} -I${.OBJDIR} -I${.CURDIR}
 CPPFLAGS+=	-D_STANDALONE



CVS commit: src/sys/arch/amd64/stand/prekern

2018-05-25 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 25 16:01:31 UTC 2018

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S

Log Message:
Hide a bunch of local symbols.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/amd64/stand/prekern/locore.S

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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.7 src/sys/arch/amd64/stand/prekern/locore.S:1.8
--- src/sys/arch/amd64/stand/prekern/locore.S:1.7	Fri Dec 22 07:37:27 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Fri May 25 16:01:31 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.7 2017/12/22 07:37:27 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.8 2018/05/25 16:01:31 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -230,7 +230,7 @@ ENTRY(start)
 	/* Load 'bootinfo' */
 	movl	12(%esp),%eax
 	testl	%eax,%eax		/* bootinfo = NULL? */
-	jz	bootinfo_finished
+	jz	.Lbootinfo_finished
 
 	movl	(%eax),%ebx		/* number of entries */
 	movl	$_C_LABEL(bootinfo),%ebp
@@ -239,9 +239,9 @@ ENTRY(start)
 	movl	%ebx,(%edx)
 	addl	$4,%edx
 
-bootinfo_entryloop:
+.Lbootinfo_entryloop:
 	testl	%ebx,%ebx		/* no remaining entries? */
-	jz	bootinfo_finished
+	jz	.Lbootinfo_finished
 
 	addl	$4,%eax
 	movl	(%eax),%ecx		/* address of entry */
@@ -253,7 +253,7 @@ bootinfo_entryloop:
 	movl	%edx,%edi
 	addl	(%ecx),%edx		/* update dest pointer */
 	cmpl	%ebp,%edx		/* beyond bootinfo+BOOTINFO_MAXSIZE? */
-	jg	bootinfo_overflow
+	jg	.Lbootinfo_overflow
 
 	movl	%ecx,%esi
 	movl	%eax,%ecx
@@ -280,9 +280,9 @@ bootinfo_entryloop:
 	popl	%esi
 	popl	%edi
 	subl	$1,%ebx			/* decrement the # of entries */
-	jmp	bootinfo_entryloop
+	jmp	.Lbootinfo_entryloop
 
-bootinfo_overflow:
+.Lbootinfo_overflow:
 	/*
 	 * Cleanup for overflow case. Pop the registers, and correct the number
 	 * of entries.
@@ -294,7 +294,7 @@ bootinfo_overflow:
 	movl	%ebp,%edx
 	subl	%ebx,(%edx)		/* correct the number of entries */
 
-bootinfo_finished:
+.Lbootinfo_finished:
 	/* Load 'esym' */
 	movl	16(%esp),%eax
 	movl	$_C_LABEL(esym),%ebp
@@ -331,9 +331,9 @@ bootinfo_finished:
 	movl	$0x8001,%eax
 	cpuid
 	andl	$CPUID_NOX,%edx
-	jz	no_NOX
+	jz	.Lno_NOX
 	movl	$PG_NX32,_C_LABEL(nox_flag)
-no_NOX:
+.Lno_NOX:
 
 /*
  * There are four levels of pages in amd64: PML4 -> PDP -> PD -> PT. They will
@@ -517,9 +517,9 @@ no_NOX:
 	orl	$(EFER_LME|EFER_SCE),%eax
 	movl	_C_LABEL(nox_flag),%ebx
 	cmpl	$0,%ebx
-	je 	skip_NOX
+	je 	.Lskip_NOX
 	orl	$(EFER_NXE),%eax
-skip_NOX:
+.Lskip_NOX:
 	wrmsr
 
 	/*



CVS commit: src/sys/arch/amd64/stand/prekern

2018-05-25 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri May 25 15:52:11 UTC 2018

Modified Files:
src/sys/arch/amd64/stand/prekern: prekern.c trap.S

Log Message:
Rename the entry points of the prekern, rename the array and move it into
.rodata.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/amd64/stand/prekern/prekern.c
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/trap.S

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/stand/prekern/prekern.c
diff -u src/sys/arch/amd64/stand/prekern/prekern.c:1.7 src/sys/arch/amd64/stand/prekern/prekern.c:1.8
--- src/sys/arch/amd64/stand/prekern/prekern.c:1.7	Sun Nov 26 11:01:09 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.c	Fri May 25 15:52:11 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.c,v 1.7 2017/11/26 11:01:09 maxv Exp $	*/
+/*	$NetBSD: prekern.c,v 1.8 2018/05/25 15:52:11 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -58,7 +58,7 @@ static struct x86_64_tss prekern_tss;
 
 #define IDTVEC(name) __CONCAT(X, name)
 typedef void (vector)(void);
-extern vector *IDTVEC(exceptions)[];
+extern vector *x86_exceptions[];
 
 void fatal(char *msg)
 {
@@ -211,7 +211,7 @@ init_idt(void)
 
 	idt = (struct gate_descriptor *)
 	for (i = 0; i < NCPUIDT; i++) {
-		setgate([i], IDTVEC(exceptions)[i], 0, SDT_SYS386IGT,
+		setgate([i], x86_exceptions[i], 0, SDT_SYS386IGT,
 		SEL_KPL, GSEL(GCODE_SEL, SEL_KPL));
 	}
 

Index: src/sys/arch/amd64/stand/prekern/trap.S
diff -u src/sys/arch/amd64/stand/prekern/trap.S:1.2 src/sys/arch/amd64/stand/prekern/trap.S:1.3
--- src/sys/arch/amd64/stand/prekern/trap.S:1.2	Fri Dec 22 07:37:27 2017
+++ src/sys/arch/amd64/stand/prekern/trap.S	Fri May 25 15:52:11 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.S,v 1.2 2017/12/22 07:37:27 maxv Exp $	*/
+/*	$NetBSD: trap.S,v 1.3 2018/05/25 15:52:11 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -90,91 +90,72 @@ IDTVEC(trap09)
 	ZTRAPENTRY(T_FPOPFLT)
 IDTVEC_END(trap09)
 
-IDTVEC(trap0a)
+IDTVEC(trap10)
 	TRAPENTRY(T_TSSFLT)
-IDTVEC_END(trap0a)
+IDTVEC_END(trap10)
 
-IDTVEC(trap0b)		/* #NP() Segment not present */
+IDTVEC(trap11)
 	ZTRAPENTRY(T_SEGNPFLT)
-IDTVEC_END(trap0b)
+IDTVEC_END(trap11)
 
-IDTVEC(trap0c)		/* #SS() Stack exception */
+IDTVEC(trap12)
 	ZTRAPENTRY(T_STKFLT)
-IDTVEC_END(trap0c)
+IDTVEC_END(trap12)
 
-IDTVEC(trap0d)		/* #GP() General protection */
+IDTVEC(trap13)
 	ZTRAPENTRY(T_PROTFLT)
-IDTVEC_END(trap0d)
+IDTVEC_END(trap13)
 
-IDTVEC(trap0e)
+IDTVEC(trap14)
 	TRAPENTRY(T_PAGEFLT)
-IDTVEC_END(trap0e)
+IDTVEC_END(trap14)
 
-IDTVEC(trap0f)
+IDTVEC(trap15)
 	ZTRAPENTRY(T_ASTFLT)
-IDTVEC_END(trap0f)
+IDTVEC_END(trap15)
 
-IDTVEC(trap10)
+IDTVEC(trap16)
 	ZTRAPENTRY(T_ARITHTRAP)
-IDTVEC_END(trap10)
+IDTVEC_END(trap16)
 
-IDTVEC(trap11)
+IDTVEC(trap17)
 	TRAPENTRY(T_ALIGNFLT)
-IDTVEC_END(trap11)
+IDTVEC_END(trap17)
 
-IDTVEC(trap12)
+IDTVEC(trap18)
 	ZTRAPENTRY(T_MCA)
-IDTVEC_END(trap12)
+IDTVEC_END(trap18)
 
-IDTVEC(trap13)
+IDTVEC(trap19)
 	ZTRAPENTRY(T_XMM)
-IDTVEC_END(trap13)
+IDTVEC_END(trap19)
 
-IDTVEC(trap14)
-IDTVEC(trap15)
-IDTVEC(trap16)
-IDTVEC(trap17)
-IDTVEC(trap18)
-IDTVEC(trap19)
-IDTVEC(trap1a)
-IDTVEC(trap1b)
-IDTVEC(trap1c)
-IDTVEC(trap1d)
-IDTVEC(trap1e)
-IDTVEC(trap1f)
+IDTVEC(trap20)
+IDTVEC(trap21)
+IDTVEC(trap22)
+IDTVEC(trap23)
+IDTVEC(trap24)
+IDTVEC(trap25)
+IDTVEC(trap26)
+IDTVEC(trap27)
+IDTVEC(trap28)
+IDTVEC(trap29)
+IDTVEC(trap30)
+IDTVEC(trap31)
 	/* 20 - 31 reserved for future exp */
 	ZTRAPENTRY(T_RESERVED)
-IDTVEC_END(trap1f)
-IDTVEC_END(trap1e)
-IDTVEC_END(trap1d)
-IDTVEC_END(trap1c)
-IDTVEC_END(trap1b)
-IDTVEC_END(trap1a)
-IDTVEC_END(trap19)
-IDTVEC_END(trap18)
-IDTVEC_END(trap17)
-IDTVEC_END(trap16)
-IDTVEC_END(trap15)
-IDTVEC_END(trap14)
-
-IDTVEC(exceptions)
-	.quad	_C_LABEL(Xtrap00), _C_LABEL(Xtrap01)
-	.quad	_C_LABEL(Xtrap02), _C_LABEL(Xtrap03)
-	.quad	_C_LABEL(Xtrap04), _C_LABEL(Xtrap05)
-	.quad	_C_LABEL(Xtrap06), _C_LABEL(Xtrap07)
-	.quad	_C_LABEL(Xtrap08), _C_LABEL(Xtrap09)
-	.quad	_C_LABEL(Xtrap0a), _C_LABEL(Xtrap0b)
-	.quad	_C_LABEL(Xtrap0c), _C_LABEL(Xtrap0d)
-	.quad	_C_LABEL(Xtrap0e), _C_LABEL(Xtrap0f)
-	.quad	_C_LABEL(Xtrap10), _C_LABEL(Xtrap11)
-	.quad	_C_LABEL(Xtrap12), _C_LABEL(Xtrap13)
-	.quad	_C_LABEL(Xtrap14), _C_LABEL(Xtrap15)
-	.quad	_C_LABEL(Xtrap16), _C_LABEL(Xtrap17)
-	.quad	_C_LABEL(Xtrap18), _C_LABEL(Xtrap19)
-	.quad	_C_LABEL(Xtrap1a), _C_LABEL(Xtrap1b)
-	.quad	_C_LABEL(Xtrap1c), _C_LABEL(Xtrap1d)
-	.quad	_C_LABEL(Xtrap1e), _C_LABEL(Xtrap1f)
-IDTVEC_END(exceptions)
+IDTVEC_END(trap31)
+IDTVEC_END(trap30)
+IDTVEC_END(trap29)
+IDTVEC_END(trap28)
+IDTVEC_END(trap27)
+IDTVEC_END(trap26)
+IDTVEC_END(trap25)
+IDTVEC_END(trap24)
+IDTVEC_END(trap23)
+IDTVEC_END(trap22)
+IDTVEC_END(trap21)
+IDTVEC_END(trap20)
 
 /*
  * Arguments pushed on the stack:
@@ -191,3 +172,24 @@ NENTRY(alltraps)
 	call	

CVS commit: src/sys/arch/amd64/stand/prekern

2018-01-15 Thread Christos Zoulas
Module Name:src
Committed By:   christos
Date:   Mon Jan 15 22:38:01 UTC 2018

Modified Files:
src/sys/arch/amd64/stand/prekern: prekern.h

Log Message:
avoid typedef redefinitiones


To generate a diff of this commit:
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.18 src/sys/arch/amd64/stand/prekern/prekern.h:1.19
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.18	Sun Nov 26 06:01:09 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Mon Jan 15 17:38:01 2018
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.18 2017/11/26 11:01:09 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.19 2018/01/15 22:38:01 christos Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -38,9 +38,6 @@
 #include "redef.h"
 
 #define ASSERT(a) if (!(a)) fatal("ASSERT");
-typedef uint64_t paddr_t;
-typedef uint64_t vaddr_t;
-typedef uint64_t pt_entry_t;
 typedef uint64_t pte_prot_t;
 #define WHITE_ON_BLACK 0x07
 #define RED_ON_BLACK 0x04



CVS commit: src/sys/arch/amd64/stand/prekern

2017-12-22 Thread Ryo ONODERA
Module Name:src
Committed By:   ryoon
Date:   Sat Dec 23 06:48:30 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile

Log Message:
Use ldscript from src to fix build.sh build


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/Makefile

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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.5 src/sys/arch/amd64/stand/prekern/Makefile:1.6
--- src/sys/arch/amd64/stand/prekern/Makefile:1.5	Sun Nov 26 11:01:09 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Sat Dec 23 06:48:30 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.5 2017/11/26 11:01:09 maxv Exp $
+#	$NetBSD: Makefile,v 1.6 2017/12/23 06:48:30 ryoon Exp $
 
 PROG=		prekern
 SRCS=		locore.S trap.S prekern.c mm.c console.c elf.c prng.c
@@ -25,7 +25,8 @@ CPPFLAGS+=	-DKERNEL -D__x86_64__
 CFLAGS+=	-Wall -Werror -Wstrict-prototypes
 CFLAGS+=	-mno-red-zone -mno-mmx -mno-sse -mno-avx -ffreestanding
 STRIPFLAG=
-LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 -T prekern.ldscript
+LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 \
+		-T ${S}/arch/amd64/stand/prekern/prekern.ldscript
 
 KERN_AS=	library
 .include	"${S}/lib/libkern/Makefile.inc"



CVS commit: src/sys/arch/amd64/stand/prekern

2017-12-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Dec 22 07:37:27 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S trap.S

Log Message:
Sync comments with reality.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/locore.S
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/trap.S

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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.6 src/sys/arch/amd64/stand/prekern/locore.S:1.7
--- src/sys/arch/amd64/stand/prekern/locore.S:1.6	Sun Nov 26 10:21:20 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Fri Dec 22 07:37:27 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.6 2017/11/26 10:21:20 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.7 2017/12/22 07:37:27 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -349,18 +349,17 @@ no_NOX:
  * --+
  *  (5)
  *
- * Virtual address space of the prekern:
- * +---+--+--+-+
- * | PREKERN IMAGE |**UNUSED**| BOOTSTRAP TABLES | ISA I/O MEM |
- * +---+--+--+-+
+ * The virtual address space is the same, since it is identity-mapped (va = pa).
+ * However, the KERNEL IMAGE is mapped as read-only: the prekern reads it, but
+ * won't write to it. (Needed when relocating the kernel.)
  *
  * PROC0 STK is obviously not linked as a page level. It just happens to be
  * caught between L4 and L3.
  *
  * (PROC0 STK + L4 + L3 + L2 + L1) is later referred to as BOOTSTRAP TABLES.
  *
- * Important note: the kernel segments are properly 4k-aligned
- * (see kern.ldscript), so there's no need to enforce alignment.
+ * Important note: the prekern segments are properly 4k-aligned
+ * (see prekern.ldscript), so there's no need to enforce alignment.
  */
 
 	/* Find end of the prekern image; brings us on (1). */

Index: src/sys/arch/amd64/stand/prekern/trap.S
diff -u src/sys/arch/amd64/stand/prekern/trap.S:1.1 src/sys/arch/amd64/stand/prekern/trap.S:1.2
--- src/sys/arch/amd64/stand/prekern/trap.S:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/trap.S	Fri Dec 22 07:37:27 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: trap.S,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: trap.S,v 1.2 2017/12/22 07:37:27 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -178,15 +178,14 @@ IDTVEC_END(exceptions)
 
 /*
  * Arguments pushed on the stack:
- *  tf_trapno
- *  tf_err: Dummy inserted if not defined
- *  tf_rip
- *  tf_cs
- *  tf_rflags
- *  tf_rsp
- *  tf_ss
+ *  sf_trapno
+ *  sf_err  (dummy inserted if not defined)
+ *  sf_rip
+ *  sf_cs
+ *  sf_rflags
+ *  sf_rsp
+ *  sf_ss
  */
-
 NENTRY(alltraps)
 	movq	%rsp,%rdi
 	call	_C_LABEL(trap)



CVS commit: src/sys/arch/amd64/stand/prekern

2017-12-21 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Dec 21 14:32:06 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Remove unused macros.


To generate a diff of this commit:
cvs rdiff -u -r1.20 -r1.21 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.20 src/sys/arch/amd64/stand/prekern/mm.c:1.21
--- src/sys/arch/amd64/stand/prekern/mm.c:1.20	Sun Nov 26 14:29:48 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Thu Dec 21 14:32:06 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.20 2017/11/26 14:29:48 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.21 2017/12/21 14:32:06 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -30,10 +30,6 @@
 
 #include "prekern.h"
 
-#define PAD_TEXT	0xCC
-#define PAD_RODATA	0x00
-#define PAD_DATA	0x00
-
 #define ELFROUND	64
 
 static const uint8_t pads[4] = {



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 26 14:29:48 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Oh, damn. Obviously I forgot one case here: an already-mapped region could
be contained entirely in the region we're trying to create. So go through
another round. While here add mm_reenter_pa, and make sure the va given to
mm_enter_pa does not already point to something.


To generate a diff of this commit:
cvs rdiff -u -r1.19 -r1.20 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.19 src/sys/arch/amd64/stand/prekern/mm.c:1.20
--- src/sys/arch/amd64/stand/prekern/mm.c:1.19	Sun Nov 26 11:01:09 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sun Nov 26 14:29:48 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.19 2017/11/26 11:01:09 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.20 2017/11/26 14:29:48 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -71,6 +71,15 @@ mm_init(paddr_t first_pa)
 static void
 mm_enter_pa(paddr_t pa, vaddr_t va, pte_prot_t prot)
 {
+	if (PTE_BASE[pl1_i(va)] & PG_V) {
+		fatal("mm_enter_pa: mapping already present");
+	}
+	PTE_BASE[pl1_i(va)] = pa | PG_V | protection_codes[prot];
+}
+
+static void
+mm_reenter_pa(paddr_t pa, vaddr_t va, pte_prot_t prot)
+{
 	PTE_BASE[pl1_i(va)] = pa | PG_V | protection_codes[prot];
 }
 
@@ -92,7 +101,7 @@ mm_palloc(size_t npages)
 
 	/* Zero them out */
 	for (i = 0; i < npages; i++) {
-		mm_enter_pa(pa + i * PAGE_SIZE, tmpva,
+		mm_reenter_pa(pa + i * PAGE_SIZE, tmpva,
 		MM_PROT_READ|MM_PROT_WRITE);
 		mm_flush_va(tmpva);
 		memset((void *)tmpva, 0, PAGE_SIZE);
@@ -120,7 +129,7 @@ mm_mprotect(vaddr_t startva, size_t size
 	for (i = 0; i < npages; i++) {
 		va = startva + i * PAGE_SIZE;
 		pa = (PTE_BASE[pl1_i(va)] & PG_FRAME);
-		mm_enter_pa(pa, va, prot);
+		mm_reenter_pa(pa, va, prot);
 		mm_flush_va(va);
 	}
 }
@@ -227,6 +236,10 @@ mm_randva_kregion(size_t size, size_t pa
 ok = false;
 break;
 			}
+			if (randva < sva && eva < (randva + size)) {
+ok = false;
+break;
+			}
 		}
 		if (ok) {
 			break;



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 26 11:08:35 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: prng.c

Log Message:
I forgot to say in my previous commit that the PRNG is inspired from a
conversation with Taylor and Thor on tech-kern@.

(just add a comment)


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/prng.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/stand/prekern/prng.c
diff -u src/sys/arch/amd64/stand/prekern/prng.c:1.1 src/sys/arch/amd64/stand/prekern/prng.c:1.2
--- src/sys/arch/amd64/stand/prekern/prng.c:1.1	Sun Nov 26 11:01:09 2017
+++ src/sys/arch/amd64/stand/prekern/prng.c	Sun Nov 26 11:08:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prng.c,v 1.1 2017/11/26 11:01:09 maxv Exp $	*/
+/*	$NetBSD: prng.c,v 1.2 2017/11/26 11:08:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -154,6 +154,7 @@ prng_init(void)
 
 	memset(, 0, sizeof(rng));
 
+	/* detect cpu features */
 	cpuid(0x07, 0x00, descs);
 	has_rdseed = (descs[1] & CPUID_SEF_RDSEED) != 0;
 	cpuid(0x01, 0x00, descs);



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 26 11:01:09 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile mm.c prekern.c prekern.h
Added Files:
src/sys/arch/amd64/stand/prekern: prng.c

Log Message:
Add a PRNG for the prekern, based on SHA512. The formula is basically:

Y0   = SHA512(entropy-file, 256bit rdseed, 64bit rdtsc)
Yn+1 = SHA512(256bit lowerhalf(Yn), 256bit rdseed, 64bit rdtsc)

On each round, random values are taken from the higher half of Yn. If
rdseed is not available, rdrand is used.

The SHA1 checksum of entropy-file is verified. However, the rndsave_t::data
field is not updated by the prekern, because the area is accessed via the
read-only view we created in locore. I like this design, so it will have
to be updated differently.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/Makefile
cvs rdiff -u -r1.18 -r1.19 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/prekern.c
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/amd64/stand/prekern/prekern.h
cvs rdiff -u -r0 -r1.1 src/sys/arch/amd64/stand/prekern/prng.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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.4 src/sys/arch/amd64/stand/prekern/Makefile:1.5
--- src/sys/arch/amd64/stand/prekern/Makefile:1.4	Fri Nov 17 07:07:52 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Sun Nov 26 11:01:09 2017
@@ -1,7 +1,7 @@
-#	$NetBSD: Makefile,v 1.4 2017/11/17 07:07:52 maxv Exp $
+#	$NetBSD: Makefile,v 1.5 2017/11/26 11:01:09 maxv Exp $
 
 PROG=		prekern
-SRCS=		locore.S trap.S prekern.c mm.c console.c elf.c
+SRCS=		locore.S trap.S prekern.c mm.c console.c elf.c prng.c
 
 NOSSP=		# defined
 NOPIE=		# defined

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.18 src/sys/arch/amd64/stand/prekern/mm.c:1.19
--- src/sys/arch/amd64/stand/prekern/mm.c:1.18	Tue Nov 21 07:56:05 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sun Nov 26 11:01:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.18 2017/11/21 07:56:05 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.19 2017/11/26 11:01:09 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -196,13 +196,6 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 	}
 }
 
-static uint64_t
-mm_rand_num64(void)
-{
-	/* XXX: yes, this is ridiculous, will be fixed soon */
-	return rdtsc();
-}
-
 static vaddr_t
 mm_randva_kregion(size_t size, size_t pagesz)
 {
@@ -213,7 +206,7 @@ mm_randva_kregion(size_t size, size_t pa
 	bool ok;
 
 	while (1) {
-		rnd = mm_rand_num64();
+		prng_get_rand(, sizeof(rnd));
 		randva = rounddown(KASLR_WINDOW_BASE +
 		rnd % (KASLR_WINDOW_SIZE - size), pagesz);
 
@@ -298,7 +291,7 @@ mm_shift_segment(vaddr_t va, size_t page
 		return 0;
 	}
 
-	rnd = mm_rand_num64();
+	prng_get_rand(, sizeof(rnd));
 	offset = roundup(rnd % shiftsize, elfalign);
 	ASSERT((va + offset) % elfalign == 0);
 
@@ -322,7 +315,7 @@ mm_map_head(void)
 	size = elf_get_head_size((vaddr_t)kernpa_start);
 	npages = size / PAGE_SIZE;
 
-	rnd = mm_rand_num64();
+	prng_get_rand(, sizeof(rnd));
 	randva = rounddown(HEAD_WINDOW_BASE + rnd % (HEAD_WINDOW_SIZE - size),
 	PAGE_SIZE);
 	mm_map_tree(randva, randva + size);

Index: src/sys/arch/amd64/stand/prekern/prekern.c
diff -u src/sys/arch/amd64/stand/prekern/prekern.c:1.6 src/sys/arch/amd64/stand/prekern/prekern.c:1.7
--- src/sys/arch/amd64/stand/prekern/prekern.c:1.6	Fri Nov 17 07:07:52 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.c	Sun Nov 26 11:01:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.c,v 1.6 2017/11/17 07:07:52 maxv Exp $	*/
+/*	$NetBSD: prekern.c,v 1.7 2017/11/26 11:01:09 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -336,6 +336,11 @@ init_prekern(paddr_t pa_start)
 	print_state(true, "Prekern loaded");
 
 	/*
+	 * Init the PRNG.
+	 */
+	prng_init();
+
+	/*
 	 * Relocate the kernel.
 	 */
 	mm_map_kernel();

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.17 src/sys/arch/amd64/stand/prekern/prekern.h:1.18
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.17	Sun Nov 26 10:21:20 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Sun Nov 26 11:01:09 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.17 2017/11/26 10:21:20 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.18 2017/11/26 11:01:09 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -111,3 +111,7 @@ void mm_map_kernel(void);
 
 /* prekern.c */
 void fatal(char *);
+
+/* prng.c */
+void prng_init(void);
+void prng_get_rand(void *, size_t);

Added files:

Index: src/sys/arch/amd64/stand/prekern/prng.c
diff -u /dev/null src/sys/arch/amd64/stand/prekern/prng.c:1.1
--- /dev/null	Sun 

CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-26 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov 26 10:21:20 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S prekern.h

Log Message:
Add rdrand.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/locore.S
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.5 src/sys/arch/amd64/stand/prekern/locore.S:1.6
--- src/sys/arch/amd64/stand/prekern/locore.S:1.5	Tue Nov 14 13:58:07 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Sun Nov 26 10:21:20 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.5 2017/11/14 13:58:07 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.6 2017/11/26 10:21:20 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -612,15 +612,26 @@ END(rdtsc)
 
 ENTRY(rdseed)
 	rdseed	%rax
-	jc	.Lsuccess
+	jc	.Lrdseed_success
 	movq	$(-1),%rax
 	ret
-.Lsuccess:
+.Lrdseed_success:
 	movq	%rax,(%rdi)
 	xorq	%rax,%rax
 	ret
 END(rdseed)
 
+ENTRY(rdrand)
+	rdrand	%rax
+	jc	.Lrdrand_success
+	movq	$(-1),%rax
+	ret
+.Lrdrand_success:
+	movq	%rax,(%rdi)
+	xorq	%rax,%rax
+	ret
+END(rdrand)
+
 ENTRY(jump_kernel)
 	movq	_C_LABEL(stkva),%rsp
 	xorq	%rbp,%rbp

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.16 src/sys/arch/amd64/stand/prekern/prekern.h:1.17
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.16	Tue Nov 21 07:56:05 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Sun Nov 26 10:21:20 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.16 2017/11/21 07:56:05 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.17 2017/11/26 10:21:20 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -100,6 +100,7 @@ void cpuid(uint32_t, uint32_t, uint32_t 
 void lidt(void *);
 uint64_t rdtsc(void);
 int rdseed(uint64_t *);
+int rdrand(uint64_t *);
 void jump_kernel(vaddr_t);
 
 /* mm.c */



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-20 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 21 07:56:05 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c prekern.h

Log Message:
Clean up and add some ASSERTs.


To generate a diff of this commit:
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.17 -r1.18 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.16 src/sys/arch/amd64/stand/prekern/elf.c:1.17
--- src/sys/arch/amd64/stand/prekern/elf.c:1.16	Fri Nov 17 07:07:52 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Tue Nov 21 07:56:05 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.16 2017/11/17 07:07:52 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.17 2017/11/21 07:56:05 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -347,6 +347,9 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 	if (i == eif.ehdr->e_shnum) {
 		fatal("elf_build_boot: symtab not found");
 	}
+	if (eif.shdr[i].sh_offset == 0) {
+		fatal("elf_build_boot: symtab not loaded");
+	}
 	eif.symtab = (Elf_Sym *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
 	eif.symcnt = eif.shdr[i].sh_size / sizeof(Elf_Sym);
 
@@ -358,6 +361,9 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 	if (eif.shdr[j].sh_type != SHT_STRTAB) {
 		fatal("elf_build_boot: wrong strtab type");
 	}
+	if (eif.shdr[j].sh_offset == 0) {
+		fatal("elf_build_boot: strtab not loaded");
+	}
 	eif.strtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
 	eif.strsz = eif.shdr[j].sh_size;
 }
@@ -380,6 +386,7 @@ elf_kernel_reloc(void)
 		eif.shdr[i].sh_type != SHT_PROGBITS) {
 			continue;
 		}
+		ASSERT(eif.shdr[i].sh_offset != 0);
 		secva = baseva + eif.shdr[i].sh_offset;
 		for (j = 0; j < eif.symcnt; j++) {
 			sym = [j];
@@ -400,9 +407,10 @@ elf_kernel_reloc(void)
 		size_t secidx, nrel;
 		uintptr_t base;
 
-		if (eif.shdr[i].sh_type != SHT_REL)
+		if (eif.shdr[i].sh_type != SHT_REL) {
 			continue;
-
+		}
+		ASSERT(eif.shdr[i].sh_offset != 0);
 		reltab = (Elf_Rel *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
 		nrel = eif.shdr[i].sh_size / sizeof(Elf_Rel);
 
@@ -428,9 +436,10 @@ elf_kernel_reloc(void)
 		size_t secidx, nrela;
 		uintptr_t base;
 
-		if (eif.shdr[i].sh_type != SHT_RELA)
+		if (eif.shdr[i].sh_type != SHT_RELA) {
 			continue;
-
+		}
+		ASSERT(eif.shdr[i].sh_offset != 0);
 		relatab = (Elf_Rela *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
 		nrela = eif.shdr[i].sh_size / sizeof(Elf_Rela);
 

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.17 src/sys/arch/amd64/stand/prekern/mm.c:1.18
--- src/sys/arch/amd64/stand/prekern/mm.c:1.17	Wed Nov 15 20:45:16 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Tue Nov 21 07:56:05 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.17 2017/11/15 20:45:16 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.18 2017/11/21 07:56:05 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -36,7 +36,7 @@
 
 #define ELFROUND	64
 
-static const int pads[4] = {
+static const uint8_t pads[4] = {
 	[BTSEG_NONE] = 0x00,
 	[BTSEG_TEXT] = 0xCC,
 	[BTSEG_RODATA] = 0x00,
@@ -107,12 +107,6 @@ mm_pte_is_valid(pt_entry_t pte)
 	return ((pte & PG_V) != 0);
 }
 
-paddr_t
-mm_vatopa(vaddr_t va)
-{
-	return (PTE_BASE[pl1_i(va)] & PG_FRAME);
-}
-
 static void
 mm_mprotect(vaddr_t startva, size_t size, pte_prot_t prot)
 {
@@ -169,9 +163,7 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 	size_t L4e_idx, L3e_idx, L2e_idx;
 	paddr_t pa;
 
-	/*
-	 * Build L4.
-	 */
+	/* Build L4. */
 	L4e_idx = pl4_i(startva);
 	nL4e = mm_nentries_range(startva, endva, NBPD_L4);
 	ASSERT(L4e_idx == 511);
@@ -181,9 +173,7 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 		L4_BASE[L4e_idx] = pa | PG_V | PG_RW;
 	}
 
-	/*
-	 * Build L3.
-	 */
+	/* Build L3. */
 	L3e_idx = pl3_i(startva);
 	nL3e = mm_nentries_range(startva, endva, NBPD_L3);
 	for (i = 0; i < nL3e; i++) {
@@ -194,9 +184,7 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 		L3_BASE[L3e_idx+i] = pa | PG_V | PG_RW;
 	}
 
-	/*
-	 * Build L2.
-	 */
+	/* Build L2. */
 	L2e_idx = pl2_i(startva);
 	nL2e = mm_nentries_range(startva, endva, NBPD_L2);
 	for (i = 0; i < nL2e; i++) {
@@ -215,39 +203,6 @@ mm_rand_num64(void)
 	return rdtsc();
 }
 
-static void
-mm_map_head(void)
-{
-	size_t i, npages, size;
-	uint64_t rnd;
-	vaddr_t randva;
-
-	/*
-	 * To get the size of the head, we give a look at the read-only
-	 * mapping of the kernel we created in locore. We're identity mapped,
-	 * so kernpa = kernva.
-	 */
-	size = elf_get_head_size((vaddr_t)kernpa_start);
-	npages = size / PAGE_SIZE;
-
-	rnd = mm_rand_num64();
-	randva = rounddown(HEAD_WINDOW_BASE + rnd % (HEAD_WINDOW_SIZE - size),
-	PAGE_SIZE);
-	

CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-16 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Nov 17 07:07:52 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile console.c elf.c pdir.h
prekern.c

Log Message:
style


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/stand/prekern/Makefile
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/console.c \
src/sys/arch/amd64/stand/prekern/pdir.h
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/prekern.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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.3 src/sys/arch/amd64/stand/prekern/Makefile:1.4
--- src/sys/arch/amd64/stand/prekern/Makefile:1.3	Tue Nov 14 07:06:34 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Fri Nov 17 07:07:52 2017
@@ -1,7 +1,7 @@
-#	$NetBSD: Makefile,v 1.3 2017/11/14 07:06:34 maxv Exp $
+#	$NetBSD: Makefile,v 1.4 2017/11/17 07:07:52 maxv Exp $
 
 PROG=		prekern
-SRCS=	locore.S trap.S prekern.c mm.c console.c elf.c
+SRCS=		locore.S trap.S prekern.c mm.c console.c elf.c
 
 NOSSP=		# defined
 NOPIE=		# defined
@@ -22,8 +22,8 @@ CPPFLAGS+=	-D_STANDALONE
 .include 
 
 CPPFLAGS+=	-DKERNEL -D__x86_64__
-CFLAGS+=	-Wall -Werror -mno-red-zone -mno-mmx -mno-sse -mno-avx -ffreestanding
-CFLAGS+=	-Wstrict-prototypes
+CFLAGS+=	-Wall -Werror -Wstrict-prototypes
+CFLAGS+=	-mno-red-zone -mno-mmx -mno-sse -mno-avx -ffreestanding
 STRIPFLAG=
 LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 -T prekern.ldscript
 

Index: src/sys/arch/amd64/stand/prekern/console.c
diff -u src/sys/arch/amd64/stand/prekern/console.c:1.2 src/sys/arch/amd64/stand/prekern/console.c:1.3
--- src/sys/arch/amd64/stand/prekern/console.c:1.2	Tue Nov 14 07:06:34 2017
+++ src/sys/arch/amd64/stand/prekern/console.c	Fri Nov 17 07:07:52 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: console.c,v 1.2 2017/11/14 07:06:34 maxv Exp $	*/
+/*	$NetBSD: console.c,v 1.3 2017/11/17 07:07:52 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -108,7 +108,7 @@ void print_state(bool ok, char *buf)
 
 void print_banner(void)
 {
-	char *banner = 
+	char *banner =
 		"   __ __\n"
 		"   \\__   \\___    |  | __ ___    \n"
 		"| ___/\\_  __ \\_/ __ \\|  |/ // __ \\_  __ \\/\\ \n"
Index: src/sys/arch/amd64/stand/prekern/pdir.h
diff -u src/sys/arch/amd64/stand/prekern/pdir.h:1.2 src/sys/arch/amd64/stand/prekern/pdir.h:1.3
--- src/sys/arch/amd64/stand/prekern/pdir.h:1.2	Sun Nov  5 16:27:18 2017
+++ src/sys/arch/amd64/stand/prekern/pdir.h	Fri Nov 17 07:07:52 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: pdir.h,v 1.2 2017/11/05 16:27:18 maxv Exp $	*/
+/*	$NetBSD: pdir.h,v 1.3 2017/11/17 07:07:52 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -28,11 +28,11 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#define PREKERNBASE		0x0
+#define PREKERNBASE	0x0
 #define PREKERNTEXTOFF	(PREKERNBASE + 0x10)
 
 #define L4_SLOT_PREKERN	0 /* pl4_i(PREKERNBASE) */
-#define L4_SLOT_PTE		255
+#define L4_SLOT_PTE	255
 
 #define PDIR_SLOT_KERN	L4_SLOT_PREKERN
 #define PDIR_SLOT_PTE	L4_SLOT_PTE

Index: src/sys/arch/amd64/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.15 src/sys/arch/amd64/stand/prekern/elf.c:1.16
--- src/sys/arch/amd64/stand/prekern/elf.c:1.15	Wed Nov 15 20:45:16 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Fri Nov 17 07:07:52 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.15 2017/11/15 20:45:16 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.16 2017/11/17 07:07:52 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -292,6 +292,7 @@ elf_map_sections(void)
 		secalign = shdr->sh_addralign;
 		ASSERT(shdr->sh_offset != 0);
 		ASSERT(secpa % PAGE_SIZE == 0);
+		ASSERT(secpa + secsz <= kernpa_end);
 
 		secva = mm_map_segment(segtype, secpa, secsz, secalign);
 

Index: src/sys/arch/amd64/stand/prekern/prekern.c
diff -u src/sys/arch/amd64/stand/prekern/prekern.c:1.5 src/sys/arch/amd64/stand/prekern/prekern.c:1.6
--- src/sys/arch/amd64/stand/prekern/prekern.c:1.5	Tue Nov 14 07:06:34 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.c	Fri Nov 17 07:07:52 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.c,v 1.5 2017/11/14 07:06:34 maxv Exp $	*/
+/*	$NetBSD: prekern.c,v 1.6 2017/11/17 07:07:52 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -46,10 +46,9 @@ struct bootinfo bootinfo;
 
 extern paddr_t kernpa_start, kernpa_end;
 
-extern uint64_t *gdt64_start;
-uint8_t idtstore[PAGE_SIZE];
-uint8_t faultstack[PAGE_SIZE];
-struct x86_64_tss prekern_tss;
+static uint8_t idtstore[PAGE_SIZE];
+static uint8_t faultstack[PAGE_SIZE];
+static struct x86_64_tss 

CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov 15 20:45:16 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c prekern.h

Log Message:
Small cleanup.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/amd64/stand/prekern/elf.c \
src/sys/arch/amd64/stand/prekern/prekern.h
cvs rdiff -u -r1.16 -r1.17 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.14 src/sys/arch/amd64/stand/prekern/elf.c:1.15
--- src/sys/arch/amd64/stand/prekern/elf.c:1.14	Wed Nov 15 18:02:36 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Wed Nov 15 20:45:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.14 2017/11/15 18:02:36 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.15 2017/11/15 20:45:16 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -372,11 +372,6 @@ elf_kernel_reloc(void)
 	print_state(true, "ELF info created");
 
 	/*
-	 * The loaded sections are: SHT_PROGBITS, SHT_NOBITS, SHT_STRTAB,
-	 * SHT_SYMTAB.
-	 */
-
-	/*
 	 * Update all symbol values with the appropriate offset.
 	 */
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.14 src/sys/arch/amd64/stand/prekern/prekern.h:1.15
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.14	Wed Nov 15 18:44:34 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Wed Nov 15 20:45:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.14 2017/11/15 18:44:34 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.15 2017/11/15 20:45:16 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -41,7 +41,6 @@
 typedef uint64_t paddr_t;
 typedef uint64_t vaddr_t;
 typedef uint64_t pt_entry_t;
-typedef uint64_t pd_entry_t;
 typedef uint64_t pte_prot_t;
 #define WHITE_ON_BLACK 0x07
 #define RED_ON_BLACK 0x04

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.16 src/sys/arch/amd64/stand/prekern/mm.c:1.17
--- src/sys/arch/amd64/stand/prekern/mm.c:1.16	Wed Nov 15 20:25:29 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Wed Nov 15 20:45:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.16 2017/11/15 20:25:29 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.17 2017/11/15 20:45:16 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -36,6 +36,13 @@
 
 #define ELFROUND	64
 
+static const int pads[4] = {
+	[BTSEG_NONE] = 0x00,
+	[BTSEG_TEXT] = 0xCC,
+	[BTSEG_RODATA] = 0x00,
+	[BTSEG_DATA] = 0x00
+};
+
 #define MM_PROT_READ	0x00
 #define MM_PROT_WRITE	0x01
 #define MM_PROT_EXECUTE	0x02
@@ -107,7 +114,7 @@ mm_vatopa(vaddr_t va)
 }
 
 static void
-mm_mprotect(vaddr_t startva, size_t size, int prot)
+mm_mprotect(vaddr_t startva, size_t size, pte_prot_t prot)
 {
 	size_t i, npages;
 	vaddr_t va;
@@ -127,7 +134,7 @@ mm_mprotect(vaddr_t startva, size_t size
 void
 mm_bootspace_mprotect(void)
 {
-	int prot;
+	pte_prot_t prot;
 	size_t i;
 
 	/* Remap the kernel segments with proper permissions. */
@@ -242,7 +249,7 @@ mm_map_head(void)
 }
 
 static vaddr_t
-mm_randva_kregion(size_t size, size_t align)
+mm_randva_kregion(size_t size, size_t pagesz)
 {
 	vaddr_t sva, eva;
 	vaddr_t randva;
@@ -253,7 +260,7 @@ mm_randva_kregion(size_t size, size_t al
 	while (1) {
 		rnd = mm_rand_num64();
 		randva = rounddown(KASLR_WINDOW_BASE +
-		rnd % (KASLR_WINDOW_SIZE - size), align);
+		rnd % (KASLR_WINDOW_SIZE - size), pagesz);
 
 		/* Detect collisions */
 		ok = true;
@@ -329,6 +336,8 @@ mm_shift_segment(vaddr_t va, size_t page
 		elfalign = ELFROUND;
 	}
 
+	ASSERT(pagesz >= elfalign);
+	ASSERT(pagesz % elfalign == 0);
 	shiftsize = roundup(elfsz, pagesz) - roundup(elfsz, elfalign);
 	if (shiftsize == 0) {
 		return 0;
@@ -368,13 +377,7 @@ mm_map_segment(int segtype, paddr_t pa, 
 	offset = mm_shift_segment(randva, pagesz, elfsz, elfalign);
 	ASSERT(offset + elfsz <= size);
 
-	if (segtype == BTSEG_TEXT) {
-		pad = PAD_TEXT;
-	} else if (segtype == BTSEG_RODATA) {
-		pad = PAD_RODATA;
-	} else {
-		pad = PAD_DATA;
-	}
+	pad = pads[segtype];
 	memset((void *)randva, pad, offset);
 	memset((void *)(randva + offset + elfsz), pad, size - elfsz - offset);
 



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov 15 20:25:29 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Mmh, should be <=.


To generate a diff of this commit:
cvs rdiff -u -r1.15 -r1.16 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.15 src/sys/arch/amd64/stand/prekern/mm.c:1.16
--- src/sys/arch/amd64/stand/prekern/mm.c:1.15	Wed Nov 15 18:44:34 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Wed Nov 15 20:25:29 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.15 2017/11/15 18:44:34 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.16 2017/11/15 20:25:29 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -350,7 +350,7 @@ mm_map_segment(int segtype, paddr_t pa, 
 	vaddr_t randva;
 	char pad;
 
-	if (elfsz < PAGE_SIZE) {
+	if (elfsz <= PAGE_SIZE) {
 		pagesz = NBPD_L1;
 	} else {
 		pagesz = NBPD_L2;



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov 15 18:44:34 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c prekern.h

Log Message:
Define MM_PROT_* locally.


To generate a diff of this commit:
cvs rdiff -u -r1.14 -r1.15 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.13 -r1.14 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.14 src/sys/arch/amd64/stand/prekern/mm.c:1.15
--- src/sys/arch/amd64/stand/prekern/mm.c:1.14	Wed Nov 15 18:02:36 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Wed Nov 15 18:44:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.14 2017/11/15 18:02:36 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.15 2017/11/15 18:44:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -36,6 +36,10 @@
 
 #define ELFROUND	64
 
+#define MM_PROT_READ	0x00
+#define MM_PROT_WRITE	0x01
+#define MM_PROT_EXECUTE	0x02
+
 static const pt_entry_t protection_codes[3] = {
 	[MM_PROT_READ] = PG_RO | PG_NX,
 	[MM_PROT_WRITE] = PG_RW | PG_NX,

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.13 src/sys/arch/amd64/stand/prekern/prekern.h:1.14
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.13	Wed Nov 15 18:02:36 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Wed Nov 15 18:44:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.13 2017/11/15 18:02:36 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.14 2017/11/15 18:44:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -37,10 +37,6 @@
 #include "pdir.h"
 #include "redef.h"
 
-#define MM_PROT_READ	0x00
-#define MM_PROT_WRITE	0x01
-#define MM_PROT_EXECUTE	0x02
-
 #define ASSERT(a) if (!(a)) fatal("ASSERT");
 typedef uint64_t paddr_t;
 typedef uint64_t vaddr_t;



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-14 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 14 13:58:08 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S redef.h

Log Message:
Remove XXX: set FRAMESIZE to the kernel value. Verily I don't understand
why we are doing that in the non-kaslr kernels, but let's just reproduce
the behavior.

jump_kernel is changed to use callq, so that the stack alignment is
preserved.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/locore.S
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/redef.h

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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.4 src/sys/arch/amd64/stand/prekern/locore.S:1.5
--- src/sys/arch/amd64/stand/prekern/locore.S:1.4	Fri Nov 10 08:05:38 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Tue Nov 14 13:58:07 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.4 2017/11/10 08:05:38 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.5 2017/11/14 13:58:07 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -623,6 +623,6 @@ END(rdseed)
 
 ENTRY(jump_kernel)
 	movq	_C_LABEL(stkva),%rsp
-	movq	$exec_kernel,%rax
-	jmpq	*%rax
+	xorq	%rbp,%rbp
+	callq	exec_kernel
 END(jump_kernel)

Index: src/sys/arch/amd64/stand/prekern/redef.h
diff -u src/sys/arch/amd64/stand/prekern/redef.h:1.1 src/sys/arch/amd64/stand/prekern/redef.h:1.2
--- src/sys/arch/amd64/stand/prekern/redef.h:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/redef.h	Tue Nov 14 13:58:07 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: redef.h,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: redef.h,v 1.2 2017/11/14 13:58:07 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -43,5 +43,4 @@
  * -- */
 
 #define PDE_SIZE 8
-#define FRAMESIZE 8 /* XXX */
-
+#define FRAMESIZE 240



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Tue Nov 14 07:06:34 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile console.c elf.c mm.c
prekern.c prekern.h

Log Message:
Add -Wstrict-prototypes, and fix each warning.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/Makefile
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/console.c
cvs rdiff -u -r1.12 -r1.13 src/sys/arch/amd64/stand/prekern/elf.c \
src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/prekern.c
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.2 src/sys/arch/amd64/stand/prekern/Makefile:1.3
--- src/sys/arch/amd64/stand/prekern/Makefile:1.2	Mon Nov 13 20:03:26 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Tue Nov 14 07:06:34 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.2 2017/11/13 20:03:26 maxv Exp $
+#	$NetBSD: Makefile,v 1.3 2017/11/14 07:06:34 maxv Exp $
 
 PROG=		prekern
 SRCS=	locore.S trap.S prekern.c mm.c console.c elf.c
@@ -23,6 +23,7 @@ CPPFLAGS+=	-D_STANDALONE
 
 CPPFLAGS+=	-DKERNEL -D__x86_64__
 CFLAGS+=	-Wall -Werror -mno-red-zone -mno-mmx -mno-sse -mno-avx -ffreestanding
+CFLAGS+=	-Wstrict-prototypes
 STRIPFLAG=
 LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 -T prekern.ldscript
 

Index: src/sys/arch/amd64/stand/prekern/console.c
diff -u src/sys/arch/amd64/stand/prekern/console.c:1.1 src/sys/arch/amd64/stand/prekern/console.c:1.2
--- src/sys/arch/amd64/stand/prekern/console.c:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/console.c	Tue Nov 14 07:06:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: console.c,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: console.c,v 1.2 2017/11/14 07:06:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -38,14 +38,14 @@ static char *cons_start;
 static size_t cons_x, cons_y;
 static char cons_buffer[CONS_WID * 2 * CONS_HEI];
 
-void init_cons()
+void init_cons(void)
 {
 	cons_start = (char *)atdevbase + (0xB8000 - IOM_BEGIN);
 	cons_x = 0;
 	cons_y = 0;
 }
 
-static void check_scroll()
+static void check_scroll(void)
 {
 	char *src, *dst;
 	size_t i;
@@ -106,7 +106,7 @@ void print_state(bool ok, char *buf)
 	print("\n");
 }
 
-void print_banner()
+void print_banner(void)
 {
 	char *banner = 
 		"   __ __\n"

Index: src/sys/arch/amd64/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.12 src/sys/arch/amd64/stand/prekern/elf.c:1.13
--- src/sys/arch/amd64/stand/prekern/elf.c:1.12	Mon Nov 13 21:33:42 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Tue Nov 14 07:06:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.12 2017/11/13 21:33:42 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.13 2017/11/14 07:06:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -50,7 +50,7 @@ static struct elfinfo eif;
 static const char entrypoint[] = "start_prekern";
 
 static int
-elf_check_header()
+elf_check_header(void)
 {
 	if (memcmp((char *)eif.ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
 	eif.ehdr->e_ident[EI_CLASS] != ELFCLASS ||
@@ -61,7 +61,7 @@ elf_check_header()
 }
 
 static vaddr_t
-elf_get_entrypoint()
+elf_get_entrypoint(void)
 {
 	Elf_Sym *sym;
 	size_t i;
@@ -259,7 +259,7 @@ elf_build_head(vaddr_t headva)
 }
 
 void
-elf_map_sections()
+elf_map_sections(void)
 {
 	const paddr_t basepa = kernpa_start;
 	const vaddr_t headva = (vaddr_t)eif.ehdr;
@@ -361,7 +361,7 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 }
 
 vaddr_t
-elf_kernel_reloc()
+elf_kernel_reloc(void)
 {
 	const vaddr_t baseva = (vaddr_t)eif.ehdr;
 	vaddr_t secva, ent;
@@ -454,7 +454,7 @@ elf_kernel_reloc()
 	/*
 	 * Get the entry point.
 	 */
-	ent = elf_get_entrypoint();
+	ent = elf_get_entrypoint();
 	if (ent == 0) {
 		fatal("elf_kernel_reloc: entry point not found");
 	}
@@ -463,4 +463,3 @@ elf_kernel_reloc()
 
 	return ent;
 }
-
Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.12 src/sys/arch/amd64/stand/prekern/mm.c:1.13
--- src/sys/arch/amd64/stand/prekern/mm.c:1.12	Mon Nov 13 21:14:04 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Tue Nov 14 07:06:34 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.12 2017/11/13 21:14:04 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.13 2017/11/14 07:06:34 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -119,7 +119,7 @@ mm_mprotect(vaddr_t startva, size_t size
 }
 
 void
-mm_bootspace_mprotect()
+mm_bootspace_mprotect(void)
 {
 	int prot;
 	size_t i;
@@ -196,14 +196,14 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 }
 
 static uint64_t
-mm_rand_num64()

CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Nov 13 21:33:42 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c

Log Message:
One more ASSERT, won't hurt.


To generate a diff of this commit:
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/amd64/stand/prekern/elf.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.11 src/sys/arch/amd64/stand/prekern/elf.c:1.12
--- src/sys/arch/amd64/stand/prekern/elf.c:1.11	Mon Nov 13 21:32:21 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Mon Nov 13 21:33:42 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.11 2017/11/13 21:32:21 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.12 2017/11/13 21:33:42 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -295,6 +295,7 @@ elf_map_sections()
 		secva = mm_map_segment(segtype, secpa, secsz);
 
 		/* We want (headva + sh_offset) to be the VA of the section. */
+		ASSERT(secva > headva);
 		shdr->sh_offset = secva - headva;
 	}
 }



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Nov 13 21:14:04 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c prekern.h

Log Message:
Change the mapping logic: don't group sections of the same type into
segments, and rather map each section independently at a random VA.

In particular, .data and .bss are not merged anymore and reside at
different addresses.


To generate a diff of this commit:
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.11 -r1.12 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.9 src/sys/arch/amd64/stand/prekern/elf.c:1.10
--- src/sys/arch/amd64/stand/prekern/elf.c:1.9	Thu Nov  9 15:56:56 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Mon Nov 13 21:14:04 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.9 2017/11/09 15:56:56 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.10 2017/11/13 21:14:04 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -258,184 +258,41 @@ elf_build_head(vaddr_t headva)
 	}
 }
 
-static bool
-elf_section_is_text(Elf_Shdr *shdr)
-{
-	if (shdr->sh_type != SHT_NOBITS &&
-	shdr->sh_type != SHT_PROGBITS) {
-		return false;
-	}
-	if (!(shdr->sh_flags & SHF_EXECINSTR)) {
-		return false;
-	}
-	return true;
-}
-
-static bool
-elf_section_is_rodata(Elf_Shdr *shdr)
-{
-	if (shdr->sh_type != SHT_NOBITS &&
-	shdr->sh_type != SHT_PROGBITS) {
-		return false;
-	}
-	if (shdr->sh_flags & (SHF_EXECINSTR|SHF_WRITE)) {
-		return false;
-	}
-	return true;
-}
-
-static bool
-elf_section_is_data(Elf_Shdr *shdr)
-{
-	if (shdr->sh_type != SHT_NOBITS &&
-	shdr->sh_type != SHT_PROGBITS) {
-		return false;
-	}
-	if (!(shdr->sh_flags & SHF_WRITE) ||
-	(shdr->sh_flags & SHF_EXECINSTR)) {
-		return false;
-	}
-	return true;
-}
-
 void
-elf_get_text(paddr_t *pa, size_t *sz)
-{
-	const paddr_t basepa = kernpa_start;
-	paddr_t minpa, maxpa, secpa;
-	size_t i, secsz;
-
-	minpa = 0x, maxpa = 0;
-	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (!elf_section_is_text([i])) {
-			continue;
-		}
-		secpa = basepa + eif.shdr[i].sh_offset;
-		secsz = eif.shdr[i].sh_size;
-		if (secpa < minpa) {
-			minpa = secpa;
-		}
-		if (secpa + secsz > maxpa) {
-			maxpa = secpa + secsz;
-		}
-	}
-	ASSERT(minpa % PAGE_SIZE == 0);
-
-	*pa = minpa;
-	*sz = maxpa - minpa;
-}
-
-void
-elf_build_text(vaddr_t textva, paddr_t textpa)
+elf_map_sections()
 {
 	const paddr_t basepa = kernpa_start;
 	const vaddr_t headva = (vaddr_t)eif.ehdr;
-	size_t i, offtext;
-
-	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (!elf_section_is_text([i])) {
-			continue;
-		}
-
-		/* Offset of the section within the text segment. */
-		offtext = basepa + eif.shdr[i].sh_offset - textpa;
-
-		/* We want (headva + sh_offset) to be the VA of the section. */
-		eif.shdr[i].sh_offset = (textva + offtext - headva);
-	}
-}
-
-void
-elf_get_rodata(paddr_t *pa, size_t *sz)
-{
-	const paddr_t basepa = kernpa_start;
-	paddr_t minpa, maxpa, secpa;
+	Elf_Shdr *shdr;
+	int segtype;
+	vaddr_t secva;
+	paddr_t secpa;
 	size_t i, secsz;
 
-	minpa = 0x, maxpa = 0;
-	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (!elf_section_is_rodata([i])) {
-			continue;
-		}
-		secpa = basepa + eif.shdr[i].sh_offset;
-		secsz = eif.shdr[i].sh_size;
-		if (secpa < minpa) {
-			minpa = secpa;
-		}
-		if (secpa + secsz > maxpa) {
-			maxpa = secpa + secsz;
-		}
-	}
-	ASSERT(minpa % PAGE_SIZE == 0);
-
-	*pa = minpa;
-	*sz = maxpa - minpa;
-}
-
-void
-elf_build_rodata(vaddr_t rodatava, paddr_t rodatapa)
-{
-	const paddr_t basepa = kernpa_start;
-	const vaddr_t headva = (vaddr_t)eif.ehdr;
-	size_t i, offrodata;
-
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (!elf_section_is_rodata([i])) {
-			continue;
-		}
-
-		/* Offset of the section within the rodata segment. */
-		offrodata = basepa + eif.shdr[i].sh_offset - rodatapa;
-
-		/* We want (headva + sh_offset) to be the VA of the section. */
-		eif.shdr[i].sh_offset = (rodatava + offrodata - headva);
-	}
-}
-
-void
-elf_get_data(paddr_t *pa, size_t *sz)
-{
-	const paddr_t basepa = kernpa_start;
-	paddr_t minpa, maxpa, secpa;
-	size_t i, secsz;
+		shdr = [i];
 
-	minpa = 0x, maxpa = 0;
-	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (!elf_section_is_data([i])) {
+		if (shdr->sh_type != SHT_NOBITS &&
+		shdr->sh_type != SHT_PROGBITS) {
 			continue;
 		}
-		secpa = basepa + eif.shdr[i].sh_offset;
-		secsz = eif.shdr[i].sh_size;
-		if (secpa < minpa) {
-			minpa = secpa;
-		}
-		if (secpa + secsz > maxpa) {
-			maxpa = secpa + secsz;
-		}
-	}
-	ASSERT(minpa % PAGE_SIZE == 0);
-
-	*pa = minpa;
-	*sz = maxpa - minpa;
-}
-
-void
-elf_build_data(vaddr_t datava, paddr_t 

CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-13 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Mon Nov 13 20:03:26 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: Makefile prekern.h

Log Message:
Link libkern in the prekern, and remove redefined functions.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/Makefile
cvs rdiff -u -r1.9 -r1.10 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/Makefile
diff -u src/sys/arch/amd64/stand/prekern/Makefile:1.1 src/sys/arch/amd64/stand/prekern/Makefile:1.2
--- src/sys/arch/amd64/stand/prekern/Makefile:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/Makefile	Mon Nov 13 20:03:26 2017
@@ -1,4 +1,4 @@
-#	$NetBSD: Makefile,v 1.1 2017/10/10 09:29:14 maxv Exp $
+#	$NetBSD: Makefile,v 1.2 2017/11/13 20:03:26 maxv Exp $
 
 PROG=		prekern
 SRCS=	locore.S trap.S prekern.c mm.c console.c elf.c
@@ -16,7 +16,8 @@ BINMODE=	444
 
 .include 
 
-CPPFLAGS+=	-I. -I${S}
+CPPFLAGS+=	-I. -I${S} -I${.OBJDIR} -I${.CURDIR}
+CPPFLAGS+=	-D_STANDALONE
 
 .include 
 
@@ -25,16 +26,25 @@ CFLAGS+=	-Wall -Werror -mno-red-zone -mn
 STRIPFLAG=
 LINKFLAGS=	-X -z max-page-size=0x10 -Ttext 0x10 -T prekern.ldscript
 
+KERN_AS=	library
+.include	"${S}/lib/libkern/Makefile.inc"
+LIBKERN=	${KERNLIB}
+
 LIBCRT0=	# nothing
 LIBCRTI=	# nothing
 LIBC=		# nothing
 LIBCRTBEGIN=	# nothing
 LIBCRTEND=	# nothing
 
-${PROG}: ${OBJS}
-	${LD} ${LINKFLAGS} -o ${.TARGET} ${OBJS}
+${PROG}: ${OBJS} ${LIBKERN}
+	${_MKTARGET_LINK}
+	${LD} ${LINKFLAGS} -o ${.TARGET} ${OBJS} ${LIBKERN}
 
 all:	${PROG}
 
 .include 
 
+cleandir distclean: .WAIT cleanlibdir
+
+cleanlibdir:
+	-rm -rf lib

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.9 src/sys/arch/amd64/stand/prekern/prekern.h:1.10
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.9	Sat Nov 11 12:51:06 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Mon Nov 13 20:03:26 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.9 2017/11/11 12:51:06 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.10 2017/11/13 20:03:26 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -31,6 +31,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 #include "pdir.h"
@@ -58,50 +59,6 @@ typedef uint64_t pte_prot_t;
 
 /* -- */
 
-static inline void
-memcpy(void *dst, void *src, size_t sz)
-{
-	char *bdst = dst, *bsrc = src;
-	while (sz > 0) {
-		*bdst = *bsrc;
-		bdst++, bsrc++, sz--;
-	}
-}
-
-static inline void
-memset(void *dst, char c, size_t sz)
-{
-	char *bdst = dst;
-	while (sz > 0) {
-		*bdst = c;
-		bdst++, sz--;
-	}
-}
-
-static inline int
-memcmp(const char *a, const char *b, size_t c)
-{
-	size_t i;
-	for (i = 0; i < c; i++) {
-		if (a[i] != b[i])
-			return 1;
-	}
-	return 0;
-}
-
-static inline int
-strcmp(char *a, char *b)
-{
-	size_t i;
-	for (i = 0; a[i] != '\0'; i++) {
-		if (a[i] != b[i])
-			return 1;
-	}
-	return 0;
-}
-
-/* -- */
-
 #define BTSEG_NONE	0
 #define BTSEG_TEXT	1
 #define BTSEG_RODATA	2



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Nov 11 13:50:57 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Detect collisions from bootspace directly.


To generate a diff of this commit:
cvs rdiff -u -r1.10 -r1.11 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.10 src/sys/arch/amd64/stand/prekern/mm.c:1.11
--- src/sys/arch/amd64/stand/prekern/mm.c:1.10	Sat Nov 11 12:51:06 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sat Nov 11 13:50:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.10 2017/11/11 12:51:06 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.11 2017/11/11 13:50:57 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -238,18 +238,12 @@ mm_map_head()
 static vaddr_t
 mm_randva_kregion(size_t size)
 {
-	static struct {
-		vaddr_t sva;
-		vaddr_t eva;
-	} regions[4];
-	static size_t idx = 0;
+	vaddr_t sva, eva;
 	vaddr_t randva;
 	uint64_t rnd;
 	size_t i;
 	bool ok;
 
-	ASSERT(idx < 4);
-
 	while (1) {
 		rnd = mm_rand_num64();
 		randva = rounddown(KASLR_WINDOW_BASE +
@@ -257,14 +251,18 @@ mm_randva_kregion(size_t size)
 
 		/* Detect collisions */
 		ok = true;
-		for (i = 0; i < idx; i++) {
-			if ((regions[i].sva <= randva) &&
-			(randva < regions[i].eva)) {
+		for (i = 0; i < BTSPACE_NSEGS; i++) {
+			if (bootspace.segs[i].type == BTSEG_NONE) {
+continue;
+			}
+			sva = bootspace.segs[i].va;
+			eva = sva + bootspace.segs[i].sz;
+
+			if ((sva <= randva) && (randva < eva)) {
 ok = false;
 break;
 			}
-			if ((regions[i].sva < randva + size) &&
-			(randva + size <= regions[i].eva)) {
+			if ((sva < randva + size) && (randva + size <= eva)) {
 ok = false;
 break;
 			}
@@ -274,10 +272,6 @@ mm_randva_kregion(size_t size)
 		}
 	}
 
-	regions[idx].eva = randva;
-	regions[idx].sva = randva + size;
-	idx++;
-
 	mm_map_tree(randva, randva + size);
 
 	return randva;



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-10 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Nov 10 08:52:57 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: prekern.h

Log Message:
Implement memcpy, the builtin version does not work with variable sizes.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.7 src/sys/arch/amd64/stand/prekern/prekern.h:1.8
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.7	Fri Nov 10 08:05:38 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Fri Nov 10 08:52:57 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.7 2017/11/10 08:05:38 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.8 2017/11/10 08:52:57 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -41,7 +41,6 @@
 #define MM_PROT_EXECUTE	0x02
 
 #define ASSERT(a) if (!(a)) fatal("ASSERT");
-#define memcpy(d, v, l) __builtin_memcpy(d, v, l)
 typedef uint64_t paddr_t;
 typedef uint64_t vaddr_t;
 typedef uint64_t pt_entry_t;
@@ -60,6 +59,16 @@ typedef uint64_t pte_prot_t;
 /* -- */
 
 static inline void
+memcpy(void *dst, void *src, size_t sz)
+{
+	char *bdst = dst, *bsrc = src;
+	while (sz > 0) {
+		*bdst = *bsrc;
+		bdst++, bsrc++, sz--;
+	}
+}
+
+static inline void
 memset(void *dst, char c, size_t sz)
 {
 	char *bdst = dst;



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-10 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Fri Nov 10 08:05:38 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S prekern.h

Log Message:
Add cpuid and rdseed.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/stand/prekern/locore.S
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.3 src/sys/arch/amd64/stand/prekern/locore.S:1.4
--- src/sys/arch/amd64/stand/prekern/locore.S:1.3	Sun Oct 29 11:28:30 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Fri Nov 10 08:05:38 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.3 2017/10/29 11:28:30 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.4 2017/11/10 08:05:38 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -583,9 +583,24 @@ END(start)
 
 /* -- */
 
+ENTRY(cpuid)
+	movq	%rbx,%r8
+	movq	%rdi,%rax
+	movq	%rsi,%rcx
+	movq	%rdx,%rsi
+	cpuid
+	movl	%eax,0(%rsi)
+	movl	%ebx,4(%rsi)
+	movl	%ecx,8(%rsi)
+	movl	%edx,12(%rsi)
+	movq	%r8,%rbx
+	ret
+END(cpuid)
+
 ENTRY(lidt)
 	lidt	(%rdi)
 	ret
+END(lidt)
 
 ENTRY(rdtsc)
 	xorq	%rax,%rax
@@ -593,9 +608,21 @@ ENTRY(rdtsc)
 	shlq	$32,%rdx
 	orq	%rdx,%rax
 	ret
+END(rdtsc)
+
+ENTRY(rdseed)
+	rdseed	%rax
+	jc	.Lsuccess
+	movq	$(-1),%rax
+	ret
+.Lsuccess:
+	movq	%rax,(%rdi)
+	xorq	%rax,%rax
+	ret
+END(rdseed)
 
 ENTRY(jump_kernel)
 	movq	_C_LABEL(stkva),%rsp
 	movq	$exec_kernel,%rax
 	jmpq	*%rax
-
+END(jump_kernel)

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.6 src/sys/arch/amd64/stand/prekern/prekern.h:1.7
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.6	Thu Nov  9 15:56:56 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Fri Nov 10 08:05:38 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.6 2017/11/09 15:56:56 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.7 2017/11/10 08:05:38 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -144,8 +144,10 @@ void elf_build_boot(vaddr_t, paddr_t);
 vaddr_t elf_kernel_reloc();
 
 /* locore.S */
+void cpuid(uint32_t, uint32_t, uint32_t *);
 void lidt(void *);
 uint64_t rdtsc();
+int rdseed(uint64_t *);
 void jump_kernel();
 
 /* mm.c */



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-09 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Nov  9 15:56:56 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c prekern.h

Log Message:
Define utility functions as inlines in prekern.h.


To generate a diff of this commit:
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.8 src/sys/arch/amd64/stand/prekern/elf.c:1.9
--- src/sys/arch/amd64/stand/prekern/elf.c:1.8	Thu Nov  9 15:24:39 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Thu Nov  9 15:56:56 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.8 2017/11/09 15:24:39 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.9 2017/11/09 15:56:56 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -49,29 +49,6 @@ extern paddr_t kernpa_start, kernpa_end;
 static struct elfinfo eif;
 static const char entrypoint[] = "start_prekern";
 
-/* XXX */
-static int
-memcmp(const char *a, const char *b, size_t c)
-{
-	size_t i;
-	for (i = 0; i < c; i++) {
-		if (a[i] != b[i])
-			return 1;
-	}
-	return 0;
-}
-static int
-strcmp(char *a, char *b)
-{
-	size_t i;
-	for (i = 0; a[i] != '\0'; i++) {
-		if (a[i] != b[i])
-			return 1;
-	}
-	return 0;
-}
-
-
 static int
 elf_check_header()
 {

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.5 src/sys/arch/amd64/stand/prekern/prekern.h:1.6
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.5	Thu Nov  9 15:24:39 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Thu Nov  9 15:56:56 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.5 2017/11/09 15:24:39 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.6 2017/11/09 15:56:56 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -69,6 +69,28 @@ memset(void *dst, char c, size_t sz)
 	}
 }
 
+static inline int
+memcmp(const char *a, const char *b, size_t c)
+{
+	size_t i;
+	for (i = 0; i < c; i++) {
+		if (a[i] != b[i])
+			return 1;
+	}
+	return 0;
+}
+
+static inline int
+strcmp(char *a, char *b)
+{
+	size_t i;
+	for (i = 0; a[i] != '\0'; i++) {
+		if (a[i] != b[i])
+			return 1;
+	}
+	return 0;
+}
+
 /* -- */
 
 struct bootspace {



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-09 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Thu Nov  9 15:24:39 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c prekern.h

Log Message:
Fill in the page padding. Only .text is pre-filled by the ld script, but
this will change in the future.


To generate a diff of this commit:
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.8 -r1.9 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.7 src/sys/arch/amd64/stand/prekern/elf.c:1.8
--- src/sys/arch/amd64/stand/prekern/elf.c:1.7	Sun Nov  5 16:26:15 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Thu Nov  9 15:24:39 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.7 2017/11/05 16:26:15 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.8 2017/11/09 15:24:39 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -345,7 +345,7 @@ elf_get_text(paddr_t *pa, size_t *sz)
 	ASSERT(minpa % PAGE_SIZE == 0);
 
 	*pa = minpa;
-	*sz = roundup(maxpa - minpa, PAGE_SIZE);
+	*sz = maxpa - minpa;
 }
 
 void
@@ -392,7 +392,7 @@ elf_get_rodata(paddr_t *pa, size_t *sz)
 	ASSERT(minpa % PAGE_SIZE == 0);
 
 	*pa = minpa;
-	*sz = roundup(maxpa - minpa, PAGE_SIZE);
+	*sz = maxpa - minpa;
 }
 
 void
@@ -439,7 +439,7 @@ elf_get_data(paddr_t *pa, size_t *sz)
 	ASSERT(minpa % PAGE_SIZE == 0);
 
 	*pa = minpa;
-	*sz = roundup(maxpa - minpa, PAGE_SIZE);
+	*sz = maxpa - minpa;
 }
 
 void

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.8 src/sys/arch/amd64/stand/prekern/mm.c:1.9
--- src/sys/arch/amd64/stand/prekern/mm.c:1.8	Sun Nov  5 16:26:15 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Thu Nov  9 15:24:39 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.8 2017/11/05 16:26:15 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.9 2017/11/09 15:24:39 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -30,6 +30,10 @@
 
 #include "prekern.h"
 
+#define PAD_TEXT	0xCC
+#define PAD_RODATA	0x00
+#define PAD_DATA	0x00
+
 static const pt_entry_t protection_codes[3] = {
 	[MM_PROT_READ] = PG_RO | PG_NX,
 	[MM_PROT_WRITE] = PG_RW | PG_NX,
@@ -275,14 +279,15 @@ mm_randva_kregion(size_t size)
 static void
 mm_map_segments()
 {
-	size_t i, npages, size;
+	size_t i, npages, size, elfsz;
 	vaddr_t randva;
 	paddr_t pa;
 
 	/*
 	 * Kernel text segment.
 	 */
-	elf_get_text(, );
+	elf_get_text(, );
+	size = roundup(elfsz, PAGE_SIZE);
 	randva = mm_randva_kregion(size);
 	npages = size / PAGE_SIZE;
 
@@ -293,6 +298,9 @@ mm_map_segments()
 	}
 	elf_build_text(randva, pa);
 
+	/* Fill in the padding */
+	memset((void *)(randva + elfsz), PAD_TEXT, size - elfsz);
+
 	/* Register the values in bootspace */
 	bootspace.text.va = randva;
 	bootspace.text.pa = pa;
@@ -301,7 +309,8 @@ mm_map_segments()
 	/*
 	 * Kernel rodata segment.
 	 */
-	elf_get_rodata(, );
+	elf_get_rodata(, );
+	size = roundup(elfsz, PAGE_SIZE);
 	randva = mm_randva_kregion(size);
 	npages = size / PAGE_SIZE;
 
@@ -312,6 +321,9 @@ mm_map_segments()
 	}
 	elf_build_rodata(randva, pa);
 
+	/* Fill in the padding */
+	memset((void *)(randva + elfsz), PAD_RODATA, size - elfsz);
+
 	/* Register the values in bootspace */
 	bootspace.rodata.va = randva;
 	bootspace.rodata.pa = pa;
@@ -320,7 +332,8 @@ mm_map_segments()
 	/*
 	 * Kernel data segment.
 	 */
-	elf_get_data(, );
+	elf_get_data(, );
+	size = roundup(elfsz, PAGE_SIZE);
 	randva = mm_randva_kregion(size);
 	npages = size / PAGE_SIZE;
 
@@ -331,6 +344,9 @@ mm_map_segments()
 	}
 	elf_build_data(randva, pa);
 
+	/* Fill in the padding */
+	memset((void *)(randva + elfsz), PAD_DATA, size - elfsz);
+
 	/* Register the values in bootspace */
 	bootspace.data.va = randva;
 	bootspace.data.pa = pa;

Index: src/sys/arch/amd64/stand/prekern/prekern.h
diff -u src/sys/arch/amd64/stand/prekern/prekern.h:1.4 src/sys/arch/amd64/stand/prekern/prekern.h:1.5
--- src/sys/arch/amd64/stand/prekern/prekern.h:1.4	Sun Nov  5 16:26:15 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.h	Thu Nov  9 15:24:39 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.h,v 1.4 2017/11/05 16:26:15 maxv Exp $	*/
+/*	$NetBSD: prekern.h,v 1.5 2017/11/09 15:24:39 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -41,7 +41,6 @@
 #define MM_PROT_EXECUTE	0x02
 
 #define ASSERT(a) if (!(a)) fatal("ASSERT");
-#define memset(d, v, l) __builtin_memset(d, v, l)
 #define memcpy(d, v, l) __builtin_memcpy(d, v, l)
 typedef uint64_t paddr_t;
 typedef uint64_t vaddr_t;
@@ -60,6 +59,18 @@ typedef uint64_t pte_prot_t;
 
 /* -- */
 
+static inline void
+memset(void *dst, char c, size_t sz)
+{
+	char *bdst = 

CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov  5 16:27:18 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: pdir.h

Log Message:
Remove unused.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/pdir.h

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/stand/prekern/pdir.h
diff -u src/sys/arch/amd64/stand/prekern/pdir.h:1.1 src/sys/arch/amd64/stand/prekern/pdir.h:1.2
--- src/sys/arch/amd64/stand/prekern/pdir.h:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/pdir.h	Sun Nov  5 16:27:18 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: pdir.h,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: pdir.h,v 1.2 2017/11/05 16:27:18 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -44,13 +44,6 @@
 #define L3_BASE	((pd_entry_t *)((char *)L2_BASE + L4_SLOT_PTE * NBPD_L2))
 #define L4_BASE	((pd_entry_t *)((char *)L3_BASE + L4_SLOT_PTE * NBPD_L1))
 
-#define PDP_BASE	L4_BASE
-
-#define NKL4_MAX_ENTRIES	(unsigned long)1
-#define NKL3_MAX_ENTRIES	(unsigned long)(NKL4_MAX_ENTRIES * 512)
-#define NKL2_MAX_ENTRIES	(unsigned long)(NKL3_MAX_ENTRIES * 512)
-#define NKL1_MAX_ENTRIES	(unsigned long)(NKL2_MAX_ENTRIES * 512)
-
 #define NKL4_KIMG_ENTRIES	1
 #define NKL3_KIMG_ENTRIES	1
 #define NKL2_KIMG_ENTRIES	32



CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-05 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Nov  5 16:26:15 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c prekern.c prekern.h

Log Message:
Mprotect the segments in mm.c using bootspace, and remove the now unused
fields of elfinfo.


To generate a diff of this commit:
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.7 -r1.8 src/sys/arch/amd64/stand/prekern/mm.c
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/stand/prekern/prekern.c \
src/sys/arch/amd64/stand/prekern/prekern.h

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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.6 src/sys/arch/amd64/stand/prekern/elf.c:1.7
--- src/sys/arch/amd64/stand/prekern/elf.c:1.6	Wed Nov  1 17:00:17 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Sun Nov  5 16:26:15 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.6 2017/11/01 17:00:17 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.7 2017/11/05 16:26:15 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -42,18 +42,6 @@ struct elfinfo {
 	size_t symcnt;
 	char *strtab;
 	size_t strsz;
-	struct {
-		vaddr_t va;
-		size_t sz;
-	} text;
-	struct {
-		vaddr_t va;
-		size_t sz;
-	} rodata;
-	struct {
-		vaddr_t va;
-		size_t sz;
-	} data;
 };
 
 extern paddr_t kernpa_start, kernpa_end;
@@ -361,15 +349,12 @@ elf_get_text(paddr_t *pa, size_t *sz)
 }
 
 void
-elf_build_text(vaddr_t textva, paddr_t textpa, size_t textsz)
+elf_build_text(vaddr_t textva, paddr_t textpa)
 {
 	const paddr_t basepa = kernpa_start;
 	const vaddr_t headva = (vaddr_t)eif.ehdr;
 	size_t i, offtext;
 
-	eif.text.va = textva;
-	eif.text.sz = textsz;
-
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
 		if (!elf_section_is_text([i])) {
 			continue;
@@ -379,7 +364,7 @@ elf_build_text(vaddr_t textva, paddr_t t
 		offtext = basepa + eif.shdr[i].sh_offset - textpa;
 
 		/* We want (headva + sh_offset) to be the VA of the section. */
-		eif.shdr[i].sh_offset = (eif.text.va + offtext - headva);
+		eif.shdr[i].sh_offset = (textva + offtext - headva);
 	}
 }
 
@@ -411,15 +396,12 @@ elf_get_rodata(paddr_t *pa, size_t *sz)
 }
 
 void
-elf_build_rodata(vaddr_t rodatava, paddr_t rodatapa, size_t rodatasz)
+elf_build_rodata(vaddr_t rodatava, paddr_t rodatapa)
 {
 	const paddr_t basepa = kernpa_start;
 	const vaddr_t headva = (vaddr_t)eif.ehdr;
 	size_t i, offrodata;
 
-	eif.rodata.va = rodatava;
-	eif.rodata.sz = rodatasz;
-
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
 		if (!elf_section_is_rodata([i])) {
 			continue;
@@ -429,7 +411,7 @@ elf_build_rodata(vaddr_t rodatava, paddr
 		offrodata = basepa + eif.shdr[i].sh_offset - rodatapa;
 
 		/* We want (headva + sh_offset) to be the VA of the section. */
-		eif.shdr[i].sh_offset = (eif.rodata.va + offrodata - headva);
+		eif.shdr[i].sh_offset = (rodatava + offrodata - headva);
 	}
 }
 
@@ -461,15 +443,12 @@ elf_get_data(paddr_t *pa, size_t *sz)
 }
 
 void
-elf_build_data(vaddr_t datava, paddr_t datapa, size_t datasz)
+elf_build_data(vaddr_t datava, paddr_t datapa)
 {
 	const paddr_t basepa = kernpa_start;
 	const vaddr_t headva = (vaddr_t)eif.ehdr;
 	size_t i, offdata;
 
-	eif.data.va = datava;
-	eif.data.sz = datasz;
-
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
 		if (!elf_section_is_data([i])) {
 			continue;
@@ -479,7 +458,7 @@ elf_build_data(vaddr_t datava, paddr_t d
 		offdata = basepa + eif.shdr[i].sh_offset - datapa;
 
 		/* We want (headva + sh_offset) to be the VA of the section. */
-		eif.shdr[i].sh_offset = (eif.data.va + offdata - headva);
+		eif.shdr[i].sh_offset = (datava + offdata - headva);
 	}
 }
 
@@ -644,15 +623,6 @@ elf_kernel_reloc()
 
 	print_state(true, "Entry point found");
 
-	/*
-	 * Remap the code segments with proper permissions.
-	 */
-	mm_mprotect(eif.text.va, eif.text.sz, MM_PROT_READ|MM_PROT_EXECUTE);
-	mm_mprotect(eif.rodata.va, eif.rodata.sz, MM_PROT_READ);
-	mm_mprotect(eif.data.va, eif.data.sz, MM_PROT_READ|MM_PROT_WRITE);
-
-	print_state(true, "Segments protection updated");
-
 	return ent;
 }
 

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.7 src/sys/arch/amd64/stand/prekern/mm.c:1.8
--- src/sys/arch/amd64/stand/prekern/mm.c:1.7	Sun Oct 29 11:38:43 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sun Nov  5 16:26:15 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.7 2017/10/29 11:38:43 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.8 2017/11/05 16:26:15 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -96,7 +96,7 @@ mm_vatopa(vaddr_t va)
 	return (PTE_BASE[pl1_i(va)] & PG_FRAME);
 }
 
-void
+static void
 mm_mprotect(vaddr_t startva, size_t size, int prot)
 {
 	size_t i, npages;
@@ -114,6 +114,20 @@ mm_mprotect(vaddr_t startva, size_t size
 	}
 }
 
+void
+mm_bootspace_mprotect()
+{
+	/*
+	 * Remap the kernel segments with proper 

CVS commit: src/sys/arch/amd64/stand/prekern

2017-11-01 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Nov  1 17:00:18 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c

Log Message:
Handle absolute symbols. Since my linux_sigcode.S::rev1.4 there are two
Elf_Rela that point to the NULL symbol - which the prekern thought was an
external reference.

In the ELF spec, STN_UNDEF means the value of the symbol is zero.


To generate a diff of this commit:
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/elf.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.5 src/sys/arch/amd64/stand/prekern/elf.c:1.6
--- src/sys/arch/amd64/stand/prekern/elf.c:1.5	Sun Oct 29 11:38:43 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Wed Nov  1 17:00:17 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.5 2017/10/29 11:38:43 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.6 2017/11/01 17:00:17 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -147,6 +147,10 @@ elf_sym_lookup(size_t symidx)
 	char *buf, *secname;
 	Elf_Shdr *sec;
 
+	if (symidx == STN_UNDEF) {
+		return 0;
+	}
+
 	if (symidx >= eif.symcnt) {
 		fatal("elf_sym_lookup: symbol beyond table");
 	}



CVS commit: src/sys/arch/amd64/stand/prekern

2017-10-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 29 11:38:43 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c mm.c

Log Message:
Fix a few error messages, and be a little more verbose.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.6 -r1.7 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.4 src/sys/arch/amd64/stand/prekern/elf.c:1.5
--- src/sys/arch/amd64/stand/prekern/elf.c:1.4	Sun Oct 29 11:28:30 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Sun Oct 29 11:38:43 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.4 2017/10/29 11:28:30 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.5 2017/10/29 11:38:43 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -285,7 +285,7 @@ elf_build_head(vaddr_t headva)
 	eif.shdr = (Elf_Shdr *)((uint8_t *)eif.ehdr + eif.ehdr->e_shoff);
 
 	if (elf_check_header() == -1) {
-		fatal("elf_build_info: wrong kernel ELF header");
+		fatal("elf_build_head: wrong kernel ELF header");
 	}
 }
 
@@ -508,10 +508,10 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 	/* Locate the section names */
 	j = eif.ehdr->e_shstrndx;
 	if (j == SHN_UNDEF) {
-		fatal("elf_build_info: shstrtab not found");
+		fatal("elf_build_boot: shstrtab not found");
 	}
 	if (j >= eif.ehdr->e_shnum) {
-		fatal("elf_build_info: wrong shstrtab index");
+		fatal("elf_build_boot: wrong shstrtab index");
 	}
 	eif.shstrtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
 	eif.shstrsz = eif.shdr[j].sh_size;
@@ -522,7 +522,7 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 			break;
 	}
 	if (i == eif.ehdr->e_shnum) {
-		fatal("elf_build_info: symtab not found");
+		fatal("elf_build_boot: symtab not found");
 	}
 	eif.symtab = (Elf_Sym *)((uint8_t *)eif.ehdr + eif.shdr[i].sh_offset);
 	eif.symcnt = eif.shdr[i].sh_size / sizeof(Elf_Sym);
@@ -530,10 +530,10 @@ elf_build_boot(vaddr_t bootva, paddr_t b
 	/* Also locate the string table */
 	j = eif.shdr[i].sh_link;
 	if (j == SHN_UNDEF || j >= eif.ehdr->e_shnum) {
-		fatal("elf_build_info: wrong strtab index");
+		fatal("elf_build_boot: wrong strtab index");
 	}
 	if (eif.shdr[j].sh_type != SHT_STRTAB) {
-		fatal("elf_build_info: wrong strtab type");
+		fatal("elf_build_boot: wrong strtab type");
 	}
 	eif.strtab = (char *)((uint8_t *)eif.ehdr + eif.shdr[j].sh_offset);
 	eif.strsz = eif.shdr[j].sh_size;

Index: src/sys/arch/amd64/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.6 src/sys/arch/amd64/stand/prekern/mm.c:1.7
--- src/sys/arch/amd64/stand/prekern/mm.c:1.6	Sun Oct 29 11:28:30 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sun Oct 29 11:38:43 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.6 2017/10/29 11:28:30 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.7 2017/10/29 11:38:43 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -385,7 +385,10 @@ mm_map_kernel()
 {
 	memset(, 0, sizeof(bootspace));
 	mm_map_head();
+	print_state(true, "Head region mapped");
 	mm_map_segments();
+	print_state(true, "Segments mapped");
 	mm_map_boot();
+	print_state(true, "Boot region mapped");
 }
 



CVS commit: src/sys/arch/amd64/stand/prekern

2017-10-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 29 11:28:30 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c locore.S mm.c prekern.c
prekern.h

Log Message:
Randomize the kernel segments independently. That is to say, put text,
rodata and data at different addresses (and in a random order).

To achieve that, the mapping order in the prekern is changed. Until now,
we were creating the kernel map the following way:
-> choose a random VA
-> map [kernpa_start; kernpa_end[ at this VA
-> parse the ELF structures from there
-> determine where exactly the kernel segments are located
-> relocate etc
Now, we are doing:
-> create a read-only view of [kernpa_start; kernpa_end[
-> from this view, compute the size of the "head" region
-> choose a random VA in the HEAD window, and map the head there
-> for each region in (text, rodata, data, boot)
-> compute the size of the region from the RO view
-> choose a random VA in the KASLR window
-> map the region there
-> relocate etc

Each time we map a region, we initialize its bootspace fields right away.

The "head" region must be put before the other regions in memory, because
the kernel uses (headva + sh_offset) to get the addresses of the symbols,
and the offset is unsigned.

Given that the head does not have an mcmodel constraint, its location is
randomized in a window located below the KASLR window.

The rest of the regions being in the same window, we need to detect
collisions.

Note that the module map is embedded in the "boot" region, and that
therefore its location is randomized too.


To generate a diff of this commit:
cvs rdiff -u -r1.3 -r1.4 src/sys/arch/amd64/stand/prekern/elf.c
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/locore.S \
src/sys/arch/amd64/stand/prekern/prekern.c \
src/sys/arch/amd64/stand/prekern/prekern.h
cvs rdiff -u -r1.5 -r1.6 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.3 src/sys/arch/amd64/stand/prekern/elf.c:1.4
--- src/sys/arch/amd64/stand/prekern/elf.c:1.3	Sun Oct 29 10:07:08 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Sun Oct 29 11:28:30 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.3 2017/10/29 10:07:08 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.4 2017/10/29 11:28:30 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -56,6 +56,8 @@ struct elfinfo {
 	} data;
 };
 
+extern paddr_t kernpa_start, kernpa_end;
+
 static struct elfinfo eif;
 static const char entrypoint[] = "start_prekern";
 
@@ -256,6 +258,37 @@ elf_apply_reloc(uintptr_t relocbase, con
 	}
 }
 
+/* -- */
+
+size_t
+elf_get_head_size(vaddr_t headva)
+{
+	Elf_Ehdr *ehdr;
+	Elf_Shdr *shdr;
+	size_t size;
+
+	ehdr = (Elf_Ehdr *)headva;
+	shdr = (Elf_Shdr *)((uint8_t *)ehdr + ehdr->e_shoff);
+
+	size = (vaddr_t)shdr + (vaddr_t)(ehdr->e_shnum * sizeof(Elf_Shdr)) -
+	(vaddr_t)ehdr;
+
+	return roundup(size, PAGE_SIZE);
+}
+
+void
+elf_build_head(vaddr_t headva)
+{
+	memset(, 0, sizeof(struct elfinfo));
+
+	eif.ehdr = (Elf_Ehdr *)headva;
+	eif.shdr = (Elf_Shdr *)((uint8_t *)eif.ehdr + eif.ehdr->e_shoff);
+
+	if (elf_check_header() == -1) {
+		fatal("elf_build_info: wrong kernel ELF header");
+	}
+}
+
 static bool
 elf_section_is_text(Elf_Shdr *shdr)
 {
@@ -296,20 +329,180 @@ elf_section_is_data(Elf_Shdr *shdr)
 	return true;
 }
 
-static void
-elf_build_info(vaddr_t baseva)
+void
+elf_get_text(paddr_t *pa, size_t *sz)
 {
-	vaddr_t secva, minva, maxva;
-	size_t secsz;
-	size_t i, j;
+	const paddr_t basepa = kernpa_start;
+	paddr_t minpa, maxpa, secpa;
+	size_t i, secsz;
 
-	memset(, 0, sizeof(struct elfinfo));
+	minpa = 0x, maxpa = 0;
+	for (i = 0; i < eif.ehdr->e_shnum; i++) {
+		if (!elf_section_is_text([i])) {
+			continue;
+		}
+		secpa = basepa + eif.shdr[i].sh_offset;
+		secsz = eif.shdr[i].sh_size;
+		if (secpa < minpa) {
+			minpa = secpa;
+		}
+		if (secpa + secsz > maxpa) {
+			maxpa = secpa + secsz;
+		}
+	}
+	ASSERT(minpa % PAGE_SIZE == 0);
 
-	eif.ehdr = (Elf_Ehdr *)baseva;
-	eif.shdr = (Elf_Shdr *)((uint8_t *)eif.ehdr + eif.ehdr->e_shoff);
+	*pa = minpa;
+	*sz = roundup(maxpa - minpa, PAGE_SIZE);
+}
 
-	if (elf_check_header() == -1) {
-		fatal("elf_build_info: wrong kernel ELF header");
+void
+elf_build_text(vaddr_t textva, paddr_t textpa, size_t textsz)
+{
+	const paddr_t basepa = kernpa_start;
+	const vaddr_t headva = (vaddr_t)eif.ehdr;
+	size_t i, offtext;
+
+	eif.text.va = textva;
+	eif.text.sz = textsz;
+
+	for (i = 0; i < eif.ehdr->e_shnum; i++) {
+		if (!elf_section_is_text([i])) {
+			continue;
+		}
+
+		/* Offset of 

CVS commit: src/sys/arch/amd64/stand/prekern

2017-10-29 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 29 10:07:08 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c

Log Message:
Add three functions and start using them; will be more useful soon.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/elf.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.2 src/sys/arch/amd64/stand/prekern/elf.c:1.3
--- src/sys/arch/amd64/stand/prekern/elf.c:1.2	Wed Oct 11 16:21:06 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Sun Oct 29 10:07:08 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.2 2017/10/11 16:21:06 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.3 2017/10/29 10:07:08 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -256,6 +256,46 @@ elf_apply_reloc(uintptr_t relocbase, con
 	}
 }
 
+static bool
+elf_section_is_text(Elf_Shdr *shdr)
+{
+	if (shdr->sh_type != SHT_NOBITS &&
+	shdr->sh_type != SHT_PROGBITS) {
+		return false;
+	}
+	if (!(shdr->sh_flags & SHF_EXECINSTR)) {
+		return false;
+	}
+	return true;
+}
+
+static bool
+elf_section_is_rodata(Elf_Shdr *shdr)
+{
+	if (shdr->sh_type != SHT_NOBITS &&
+	shdr->sh_type != SHT_PROGBITS) {
+		return false;
+	}
+	if (shdr->sh_flags & (SHF_EXECINSTR|SHF_WRITE)) {
+		return false;
+	}
+	return true;
+}
+
+static bool
+elf_section_is_data(Elf_Shdr *shdr)
+{
+	if (shdr->sh_type != SHT_NOBITS &&
+	shdr->sh_type != SHT_PROGBITS) {
+		return false;
+	}
+	if (!(shdr->sh_flags & SHF_WRITE) ||
+	(shdr->sh_flags & SHF_EXECINSTR)) {
+		return false;
+	}
+	return true;
+}
+
 static void
 elf_build_info(vaddr_t baseva)
 {
@@ -314,11 +354,7 @@ elf_build_info(vaddr_t baseva)
 	/* text */
 	minva = 0x, maxva = 0;
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (eif.shdr[i].sh_type != SHT_NOBITS &&
-		eif.shdr[i].sh_type != SHT_PROGBITS) {
-			continue;
-		}
-		if (!(eif.shdr[i].sh_flags & SHF_EXECINSTR)) {
+		if (!elf_section_is_text([i])) {
 			continue;
 		}
 		secva = baseva + eif.shdr[i].sh_offset;
@@ -337,11 +373,7 @@ elf_build_info(vaddr_t baseva)
 	/* rodata */
 	minva = 0x, maxva = 0;
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (eif.shdr[i].sh_type != SHT_NOBITS &&
-		eif.shdr[i].sh_type != SHT_PROGBITS) {
-			continue;
-		}
-		if ((eif.shdr[i].sh_flags & (SHF_EXECINSTR|SHF_WRITE))) {
+		if (!elf_section_is_rodata([i])) {
 			continue;
 		}
 		secva = baseva + eif.shdr[i].sh_offset;
@@ -360,12 +392,7 @@ elf_build_info(vaddr_t baseva)
 	/* data */
 	minva = 0x, maxva = 0;
 	for (i = 0; i < eif.ehdr->e_shnum; i++) {
-		if (eif.shdr[i].sh_type != SHT_NOBITS &&
-		eif.shdr[i].sh_type != SHT_PROGBITS) {
-			continue;
-		}
-		if (!(eif.shdr[i].sh_flags & SHF_WRITE) ||
-		(eif.shdr[i].sh_flags & SHF_EXECINSTR)) {
+		if (!elf_section_is_data([i])) {
 			continue;
 		}
 		secva = baseva + eif.shdr[i].sh_offset;



CVS commit: src/sys/arch/amd64/stand/prekern

2017-10-28 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sat Oct 28 19:28:11 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Fix a mistake I made in the very first revision. The calculation of the
number of slots was incorrect in some cases, and it could cause the
prekern to fault right away at boot time, or the kernel to fault when
loading kernel modules near the end of the module map.

The variables are divided by PAGE_SIZE to prevent integer overflows.


To generate a diff of this commit:
cvs rdiff -u -r1.4 -r1.5 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.4 src/sys/arch/amd64/stand/prekern/mm.c:1.5
--- src/sys/arch/amd64/stand/prekern/mm.c:1.4	Mon Oct 23 06:00:59 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sat Oct 28 19:28:11 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.4 2017/10/23 06:00:59 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.5 2017/10/28 19:28:11 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -112,20 +112,28 @@ mm_mprotect(vaddr_t startva, size_t size
 	}
 }
 
+static size_t
+mm_nentries_range(vaddr_t startva, vaddr_t endva, size_t pgsz)
+{
+	size_t npages;
+
+	npages = roundup((endva / PAGE_SIZE), (pgsz / PAGE_SIZE)) -
+	rounddown((startva / PAGE_SIZE), (pgsz / PAGE_SIZE));
+	return (npages / (pgsz / PAGE_SIZE));
+}
+
 static void
 mm_map_tree(vaddr_t startva, vaddr_t endva)
 {
-	size_t i, size, nL4e, nL3e, nL2e;
+	size_t i, nL4e, nL3e, nL2e;
 	size_t L4e_idx, L3e_idx, L2e_idx;
 	paddr_t pa;
 
-	size = endva - startva;
-
 	/*
 	 * Build L4.
 	 */
 	L4e_idx = pl4_i(startva);
-	nL4e = roundup(size, NBPD_L4) / NBPD_L4;
+	nL4e = mm_nentries_range(startva, endva, NBPD_L4);
 	ASSERT(L4e_idx == 511);
 	ASSERT(nL4e == 1);
 	if (!mm_pte_is_valid(L4_BASE[L4e_idx])) {
@@ -137,7 +145,7 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 	 * Build L3.
 	 */
 	L3e_idx = pl3_i(startva);
-	nL3e = roundup(size, NBPD_L3) / NBPD_L3;
+	nL3e = mm_nentries_range(startva, endva, NBPD_L3);
 	for (i = 0; i < nL3e; i++) {
 		if (mm_pte_is_valid(L3_BASE[L3e_idx+i])) {
 			continue;
@@ -150,7 +158,7 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 	 * Build L2.
 	 */
 	L2e_idx = pl2_i(startva);
-	nL2e = roundup(size, NBPD_L2) / NBPD_L2;
+	nL2e = mm_nentries_range(startva, endva, NBPD_L2);
 	for (i = 0; i < nL2e; i++) {
 		if (mm_pte_is_valid(L2_BASE[L2e_idx+i])) {
 			continue;



CVS commit: src/sys/arch/amd64/stand/prekern

2017-10-18 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Oct 18 17:12:42 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
If a branch is already there, use it and don't create a new one. This way
we can call mm_map_tree twice with neighboring regions.


To generate a diff of this commit:
cvs rdiff -u -r1.2 -r1.3 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.2 src/sys/arch/amd64/stand/prekern/mm.c:1.3
--- src/sys/arch/amd64/stand/prekern/mm.c:1.2	Sun Oct 15 06:37:32 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Wed Oct 18 17:12:42 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.2 2017/10/15 06:37:32 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.3 2017/10/18 17:12:42 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -82,6 +82,12 @@ mm_palloc(size_t npages)
 	return pa;
 }
 
+static bool
+mm_pte_is_valid(pt_entry_t pte)
+{
+	return ((pte & PG_V) != 0);
+}
+
 paddr_t
 mm_vatopa(vaddr_t va)
 {
@@ -111,39 +117,46 @@ mm_map_tree(vaddr_t startva, vaddr_t end
 {
 	size_t i, size, nL4e, nL3e, nL2e;
 	size_t L4e_idx, L3e_idx, L2e_idx;
-	paddr_t L3page_pa, L2page_pa, L1page_pa;
+	paddr_t pa;
+
+	size = endva - startva;
 
 	/*
-	 * Initialize constants.
+	 * Build L4.
 	 */
-	size = endva - startva;
-	nL4e = roundup(size, NBPD_L4) / NBPD_L4;
-	nL3e = roundup(size, NBPD_L3) / NBPD_L3;
-	nL2e = roundup(size, NBPD_L2) / NBPD_L2;
 	L4e_idx = pl4_i(startva);
-	L3e_idx = pl3_i(startva);
-	L2e_idx = pl2_i(startva);
-
-	ASSERT(nL4e == 1);
+	nL4e = roundup(size, NBPD_L4) / NBPD_L4;
 	ASSERT(L4e_idx == 511);
+	ASSERT(nL4e == 1);
+	if (!mm_pte_is_valid(L4_BASE[L4e_idx])) {
+		pa = mm_palloc(1);
+		L4_BASE[L4e_idx] = pa | PG_V | PG_RW;
+	}
 
 	/*
-	 * Allocate the physical pages.
+	 * Build L3.
 	 */
-	L3page_pa = mm_palloc(nL4e);
-	L2page_pa = mm_palloc(nL3e);
-	L1page_pa = mm_palloc(nL2e);
+	L3e_idx = pl3_i(startva);
+	nL3e = roundup(size, NBPD_L3) / NBPD_L3;
+	for (i = 0; i < nL3e; i++) {
+		if (mm_pte_is_valid(L3_BASE[L3e_idx+i])) {
+			continue;
+		}
+		pa = mm_palloc(1);
+		L3_BASE[L3e_idx+i] = pa | PG_V | PG_RW;
+	}
 
 	/*
-	 * Build the branch in the page tree. We link the levels together,
-	 * from L4 to L1.
+	 * Build L2.
 	 */
-	L4_BASE[L4e_idx] = L3page_pa | PG_V | PG_RW;
-	for (i = 0; i < nL3e; i++) {
-		L3_BASE[L3e_idx+i] = (L2page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
-	}
+	L2e_idx = pl2_i(startva);
+	nL2e = roundup(size, NBPD_L2) / NBPD_L2;
 	for (i = 0; i < nL2e; i++) {
-		L2_BASE[L2e_idx+i] = (L1page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
+		if (mm_pte_is_valid(L2_BASE[L2e_idx+i])) {
+			continue;
+		}
+		pa = mm_palloc(1);
+		L2_BASE[L2e_idx+i] = pa | PG_V | PG_RW;
 	}
 }
 



CVS commit: src/sys/arch/amd64/stand/prekern

2017-10-15 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Sun Oct 15 06:37:32 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: mm.c

Log Message:
Descend the page tree from L4 to L1, instead of allocating a separate
branch and linking it at the end. This way we don't need to allocate VA
from the (tiny) prekern map.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/mm.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/stand/prekern/mm.c
diff -u src/sys/arch/amd64/stand/prekern/mm.c:1.1 src/sys/arch/amd64/stand/prekern/mm.c:1.2
--- src/sys/arch/amd64/stand/prekern/mm.c:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/mm.c	Sun Oct 15 06:37:32 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: mm.c,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: mm.c,v 1.2 2017/10/15 06:37:32 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -41,8 +41,7 @@ extern paddr_t kernpa_start, kernpa_end;
 vaddr_t iom_base;
 
 paddr_t pa_avail = 0;
-static vaddr_t va_avail = (PREKERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2);
-static vaddr_t va_end = (PREKERNBASE + (NKL2_KIMG_ENTRIES + 1) * NBPD_L2);
+static const vaddr_t tmpva = (PREKERNBASE + NKL2_KIMG_ENTRIES * NBPD_L2);
 
 void
 mm_init(paddr_t first_pa)
@@ -50,25 +49,6 @@ mm_init(paddr_t first_pa)
 	pa_avail = first_pa;
 }
 
-static paddr_t
-mm_palloc(size_t npages)
-{
-	paddr_t pa = pa_avail;
-	pa_avail += npages * PAGE_SIZE;
-	return pa;
-}
-
-static vaddr_t
-mm_valloc(size_t npages)
-{
-	vaddr_t va = va_avail;
-	va_avail += npages * PAGE_SIZE;
-	if (va_avail > va_end) {
-		fatal("mm_valloc: no VA left");
-	}
-	return va;
-}
-
 static void
 mm_enter_pa(paddr_t pa, vaddr_t va, pte_prot_t prot)
 {
@@ -81,6 +61,27 @@ mm_flush_va(vaddr_t va)
 	asm volatile("invlpg (%0)" ::"r" (va) : "memory");
 }
 
+static paddr_t
+mm_palloc(size_t npages)
+{
+	paddr_t pa;
+	size_t i;
+
+	/* Allocate the physical pages */
+	pa = pa_avail;
+	pa_avail += npages * PAGE_SIZE;
+
+	/* Zero them out */
+	for (i = 0; i < npages; i++) {
+		mm_enter_pa(pa + i * PAGE_SIZE, tmpva,
+		MM_PROT_READ|MM_PROT_WRITE);
+		mm_flush_va(tmpva);
+		memset((void *)tmpva, 0, PAGE_SIZE);
+	}
+
+	return pa;
+}
+
 paddr_t
 mm_vatopa(vaddr_t va)
 {
@@ -106,13 +107,11 @@ mm_mprotect(vaddr_t startva, size_t size
 }
 
 static void
-mm_map_va(vaddr_t startva, vaddr_t endva)
+mm_map_tree(vaddr_t startva, vaddr_t endva)
 {
-	size_t i, idx, size, nL4e, nL3e, nL2e;
+	size_t i, size, nL4e, nL3e, nL2e;
 	size_t L4e_idx, L3e_idx, L2e_idx;
-	vaddr_t L3page_va, L2page_va;
 	paddr_t L3page_pa, L2page_pa, L1page_pa;
-	pd_entry_t *pdir;
 
 	/*
 	 * Initialize constants.
@@ -122,48 +121,30 @@ mm_map_va(vaddr_t startva, vaddr_t endva
 	nL3e = roundup(size, NBPD_L3) / NBPD_L3;
 	nL2e = roundup(size, NBPD_L2) / NBPD_L2;
 	L4e_idx = pl4_i(startva);
-	L3e_idx = pl3_i(startva % NBPD_L4);
-	L2e_idx = pl2_i(startva % NBPD_L3);
+	L3e_idx = pl3_i(startva);
+	L2e_idx = pl2_i(startva);
+
+	ASSERT(nL4e == 1);
+	ASSERT(L4e_idx == 511);
 
 	/*
-	 * Map the sub-tree itself.
+	 * Allocate the physical pages.
 	 */
-	L3page_va = mm_valloc(nL4e);
 	L3page_pa = mm_palloc(nL4e);
-	L2page_va = mm_valloc(nL3e);
 	L2page_pa = mm_palloc(nL3e);
-
 	L1page_pa = mm_palloc(nL2e);
 
-	for (i = 0; i < nL4e; i++) {
-		mm_enter_pa(L3page_pa + i * PAGE_SIZE,
-		L3page_va + i * PAGE_SIZE, MM_PROT_READ|MM_PROT_WRITE);
-		memset((void *)(L3page_va + i * PAGE_SIZE), 0, PAGE_SIZE);
-	}
-
-	for (i = 0; i < nL3e; i++) {
-		mm_enter_pa(L2page_pa + i * PAGE_SIZE,
-		L2page_va + i * PAGE_SIZE, MM_PROT_READ|MM_PROT_WRITE);
-		memset((void *)(L2page_va + i * PAGE_SIZE), 0, PAGE_SIZE);
-	}
-
 	/*
-	 * Now link the levels together.
+	 * Build the branch in the page tree. We link the levels together,
+	 * from L4 to L1.
 	 */
-	pdir = (pt_entry_t *)L3page_va;
-	for (i = 0, idx = L3e_idx; i < nL3e; i++, idx++) {
-		pdir[idx] = (L2page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
+	L4_BASE[L4e_idx] = L3page_pa | PG_V | PG_RW;
+	for (i = 0; i < nL3e; i++) {
+		L3_BASE[L3e_idx+i] = (L2page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
 	}
-
-	pdir = (pt_entry_t *)L2page_va;
-	for (i = 0, idx = L2e_idx; i < nL2e; i++, idx++) {
-		pdir[idx] = (L1page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
+	for (i = 0; i < nL2e; i++) {
+		L2_BASE[L2e_idx+i] = (L1page_pa + i * PAGE_SIZE) | PG_V | PG_RW;
 	}
-
-	/*
-	 * Finally, link the sub-tree into the tree.
-	 */
-	L4_BASE[L4e_idx] = L3page_pa | PG_V | PG_RW;
 }
 
 /*
@@ -185,7 +166,7 @@ mm_rand_base()
 	randva = rounddown(KASLR_WINDOW_BASE + rnd % (KASLR_WINDOW_SIZE - size),
 	PAGE_SIZE);
 
-	mm_map_va(randva, randva + size);
+	mm_map_tree(randva, randva + size);
 
 	return randva;
 }



CVS commit: src/sys/arch/amd64/stand/prekern

2017-10-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Oct 11 16:21:06 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: elf.c

Log Message:
Make sure we're relocating a relocatable kernel.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/elf.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/stand/prekern/elf.c
diff -u src/sys/arch/amd64/stand/prekern/elf.c:1.1 src/sys/arch/amd64/stand/prekern/elf.c:1.2
--- src/sys/arch/amd64/stand/prekern/elf.c:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/elf.c	Wed Oct 11 16:21:06 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: elf.c,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: elf.c,v 1.2 2017/10/11 16:21:06 maxv Exp $	*/
 
 /*
  * Copyright (c) 2017 The NetBSD Foundation, Inc. All rights reserved.
@@ -86,7 +86,8 @@ static int
 elf_check_header()
 {
 	if (memcmp((char *)eif.ehdr->e_ident, ELFMAG, SELFMAG) != 0 ||
-	eif.ehdr->e_ident[EI_CLASS] != ELFCLASS) {
+	eif.ehdr->e_ident[EI_CLASS] != ELFCLASS ||
+	eif.ehdr->e_type != ET_REL) {
 		return -1;
 	}
 	return 0;



CVS commit: src/sys/arch/amd64/stand/prekern

2017-10-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Oct 11 16:18:11 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: locore.S

Log Message:
Remove this #if, these options belong to the kernel and not the prekern.
No real change since eblob is always here. And I was apparently drunk
when writing some comments.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/locore.S

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/stand/prekern/locore.S
diff -u src/sys/arch/amd64/stand/prekern/locore.S:1.1 src/sys/arch/amd64/stand/prekern/locore.S:1.2
--- src/sys/arch/amd64/stand/prekern/locore.S:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/locore.S	Wed Oct 11 16:18:11 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: locore.S,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: locore.S,v 1.2 2017/10/11 16:18:11 maxv Exp $	*/
 
 /*
  * Copyright (c) 1998, 2000, 2007, 2008, 2016, 2017 The NetBSD Foundation, Inc.
@@ -366,21 +366,19 @@ no_NOX:
 	/* Find end of the prekern image; brings us on (1). */
 	movl	$_C_LABEL(__prekern_end),%edi
 
-	/* Find end of the kernel image; brind us on (2). */
+	/* Find end of the kernel image; brings us on (2). */
 	movl	_C_LABEL(kernpa_end),%eax
 	testl	%eax,%eax
 	jz	1f
 	movl	%eax,%edi
 1:
 
-	/* Find end of the kernel symbols; brinds us on (3). */
-#if (NKSYMS || defined(DDB) || defined(MODULAR)) && !defined(makeoptions_COPY_SYMTAB) /* XXX */
+	/* Find end of the kernel symbols; brings us on (3). */
 	movl	_C_LABEL(esym),%eax
 	testl	%eax,%eax
 	jz	1f
 	movl	%eax,%edi
 1:
-#endif
 
 	/* Find end of the kernel preloaded modules; brings us on (4). */
 	movl	_C_LABEL(eblob),%eax



CVS commit: src/sys/arch/amd64/stand/prekern

2017-10-11 Thread Maxime Villard
Module Name:src
Committed By:   maxv
Date:   Wed Oct 11 16:13:16 UTC 2017

Modified Files:
src/sys/arch/amd64/stand/prekern: prekern.ldscript

Log Message:
Add an alignment to fill strictly all of the padding; does not increase
the size of the prekern.


To generate a diff of this commit:
cvs rdiff -u -r1.1 -r1.2 src/sys/arch/amd64/stand/prekern/prekern.ldscript

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/stand/prekern/prekern.ldscript
diff -u src/sys/arch/amd64/stand/prekern/prekern.ldscript:1.1 src/sys/arch/amd64/stand/prekern/prekern.ldscript:1.2
--- src/sys/arch/amd64/stand/prekern/prekern.ldscript:1.1	Tue Oct 10 09:29:14 2017
+++ src/sys/arch/amd64/stand/prekern/prekern.ldscript	Wed Oct 11 16:13:16 2017
@@ -1,4 +1,4 @@
-/*	$NetBSD: prekern.ldscript,v 1.1 2017/10/10 09:29:14 maxv Exp $	*/
+/*	$NetBSD: prekern.ldscript,v 1.2 2017/10/11 16:13:16 maxv Exp $	*/
 
 __PAGE_SIZE = 0x1000 ;
 
@@ -10,6 +10,7 @@ SECTIONS
 		*(.text)
 		*(.text.*)
 		*(.stub)
+		. = ALIGN(__PAGE_SIZE);
 	} =0xCC
 	_etext = . ;
 	PROVIDE (etext = .) ;