Module Name:    src
Committed By:   riastradh
Date:           Mon May 11 21:38:54 UTC 2020

Modified Files:
        src/sys/kern: init_main.c subr_cprng.c

Log Message:
Move cprng_init before configure.

This makes it available to device drivers, e.g. to generate MAC
addresses at random, without initialization order hacks.

Requires a minor initialization hack for cpu_name(primary cpu) early
on, since that doesn't get set until mi_cpu_attach which may not run
until the middle of configure.  But this hack is less bad than other
initialization order hacks.


To generate a diff of this commit:
cvs rdiff -u -r1.524 -r1.525 src/sys/kern/init_main.c
cvs rdiff -u -r1.38 -r1.39 src/sys/kern/subr_cprng.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/kern/init_main.c
diff -u src/sys/kern/init_main.c:1.524 src/sys/kern/init_main.c:1.525
--- src/sys/kern/init_main.c:1.524	Thu Apr 30 03:28:18 2020
+++ src/sys/kern/init_main.c	Mon May 11 21:38:54 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: init_main.c,v 1.524 2020/04/30 03:28:18 riastradh Exp $	*/
+/*	$NetBSD: init_main.c,v 1.525 2020/05/11 21:38:54 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2008, 2009, 2019 The NetBSD Foundation, Inc.
@@ -97,7 +97,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.524 2020/04/30 03:28:18 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: init_main.c,v 1.525 2020/05/11 21:38:54 riastradh Exp $");
 
 #include "opt_ddb.h"
 #include "opt_inet.h"
@@ -395,6 +395,8 @@ main(void)
 	 */
 	rnd_init();		/* initialize entropy pool */
 
+	cprng_init();		/* initialize cryptographic PRNG */
+
 	/* Initialize process and pgrp structures. */
 	procinit();
 	lwpinit();
@@ -529,8 +531,6 @@ main(void)
 	/* Configure the system hardware.  This will enable interrupts. */
 	configure();
 
-	cprng_init();		/* initialize cryptographic PRNG */
-
 	/* Once all CPUs are detected, initialize the per-CPU cprng_fast.  */
 	cprng_fast_init();
 

Index: src/sys/kern/subr_cprng.c
diff -u src/sys/kern/subr_cprng.c:1.38 src/sys/kern/subr_cprng.c:1.39
--- src/sys/kern/subr_cprng.c:1.38	Mon May 11 17:27:48 2020
+++ src/sys/kern/subr_cprng.c	Mon May 11 21:38:54 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: subr_cprng.c,v 1.38 2020/05/11 17:27:48 riastradh Exp $	*/
+/*	$NetBSD: subr_cprng.c,v 1.39 2020/05/11 21:38:54 riastradh Exp $	*/
 
 /*-
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -52,7 +52,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.38 2020/05/11 17:27:48 riastradh Exp $");
+__KERNEL_RCSID(0, "$NetBSD: subr_cprng.c,v 1.39 2020/05/11 21:38:54 riastradh Exp $");
 
 #include <sys/types.h>
 #include <sys/cprng.h>
@@ -217,6 +217,7 @@ cprng_init_cpu(void *ptr, void *cookie, 
 {
 	struct cprng_cpu *cc = ptr;
 	const char *name = cookie;
+	const char *cpuname;
 	uint8_t zero[NIST_HASH_DRBG_SEEDLEN_BYTES] = {0};
 	char namebuf[64];	/* XXX size? */
 
@@ -246,10 +247,12 @@ cprng_init_cpu(void *ptr, void *cookie, 
 		panic("nist_hash_drbg_instantiate");
 
 	/* Attach the event counters.  */
+	/* XXX ci_cpuname may not be initialized early enough.  */
+	cpuname = ci->ci_cpuname[0] == '\0' ? "cpu0" : ci->ci_cpuname;
 	evcnt_attach_dynamic(&cc->cc_evcnt->intr, EVCNT_TYPE_MISC, NULL,
-	    ci->ci_cpuname, "cprng_strong intr");
+	    cpuname, "cprng_strong intr");
 	evcnt_attach_dynamic(&cc->cc_evcnt->reseed, EVCNT_TYPE_MISC, NULL,
-	    ci->ci_cpuname, "cprng_strong reseed");
+	    cpuname, "cprng_strong reseed");
 
 	/* Set the epoch uninitialized so we reseed on first use.  */
 	cc->cc_epoch = 0;

Reply via email to