rough-draft VKERNEL host-initiated shutdown patch

Chris Turner c.turner at 199technologies.org
Sun Jun 10 16:42:26 PDT 2007


OK.. Updated / cleaned this against -HEAD (as of this morning) and
things appear to be working.

(vk still too fast to catch the 'no init yet' case)

As suggested:

  - moved shutdown hook and sigaction setup to
    sys/platform/vkernel/i386/exception.c.

  - changed mailbox check to :

    - /sys/platform/vkernel/i386/trap.c,v : go_user()
    - /sys/platform/vkernel/i386/cpu_regs.c,v : cpu_idle()

  - kept mailbox flag in platform/vkernel/include/md_var.h

But:

  - I'm still not 100% on the how the SIGIO mailbox interlock
    works, so the mailbox check hooks might be in the wrong /
    non-optimal places within go_user() / cpu_idle().
  - There isn't really a clean way of getting at the shutdown function
    from oustide 'sys/kern/kern_shutdown.c'. This results in compiler
    warnings, but the linker sorts things out.
  - Updated vkernel manual page also attached with patch - hopefully
    initial meaning was kept.
  - Didn't implement any other signals yet as am still getting approach
    sorted out.

p.s:

   - now that there are two mailbox signals, perhaps signalmailbox()
     should and the related mdvar should be renamed e.g. iomailbox() ?
     didn't do this as not really directly related to the patch..
   - .. and mailbox signals still a bit fuzzy in the manual :)

Thanks

 - Chris
Index: share/man/man7/vkernel.7
===================================================================
RCS file: /var/local/apps/dcvs/src/share/man/man7/vkernel.7,v
retrieving revision 1.16
diff -u -r1.16 vkernel.7
--- share/man/man7/vkernel.7	25 May 2007 02:21:10 -0000	1.16
+++ share/man/man7/vkernel.7	10 Jun 2007 23:09:02 -0000
@@ -202,15 +202,17 @@
 .Xr tap 4
 device.
 .Sh SIGNALS
-The virtual kernel enables all terminal signals while in
-.Xr ddb 4
-mode but only enables
+The virtual kernel only enables SIGQUIT and SIGTERM while operating in 
+regular console mode. Sending 
 .Ql \&^\e
 .Pq Dv SIGQUIT
-while operating as a console which causes the virtual kernel to enter its
-internal
+to the virtual kernel causes the virtual kernel to enter its internal
 .Xr ddb 4
-debugger.
+debugger and re-enable all other terminal signals. Sending SIGTERM to the 
+virtual kernel triggers a clean shutdown by passing a SIGUSR2 to the 
+virtual kernel's 
+.Xr init 8
+process.
 .Sh DEBUGGING
 It is possible to directly gdb the virtual kernel's process.
 It is recommended that you do a
Index: sys/platform/vkernel/i386/cpu_regs.c
===================================================================
RCS file: /var/local/apps/dcvs/src/sys/platform/vkernel/i386/cpu_regs.c,v
retrieving revision 1.15
diff -u -r1.15 cpu_regs.c
--- sys/platform/vkernel/i386/cpu_regs.c	17 May 2007 21:08:50 -0000	1.15
+++ sys/platform/vkernel/i386/cpu_regs.c	10 Jun 2007 22:43:22 -0000
@@ -700,6 +700,8 @@
 		if (cpu_idle_hlt && !lwkt_runnable() &&
 		    (td->td_flags & TDF_IDLE_NOHLT) == 0) {
 			splz();
+	                if (mdcpu->gd_shutdown)
+				exc_shutdown();
 			signalmailbox(NULL);
 			if (!lwkt_runnable()) {
 				sigpause(0);
@@ -713,6 +715,8 @@
 		} else {
 			td->td_flags &= ~TDF_IDLE_NOHLT;
 			splz();
+	                if (mdcpu->gd_shutdown)
+				exc_shutdown();
 			signalmailbox(NULL);
 #ifdef SMP
 			/*__asm __volatile("sti; pause");*/
Index: sys/platform/vkernel/i386/exception.c
===================================================================
RCS file: /var/local/apps/dcvs/src/sys/platform/vkernel/i386/exception.c,v
retrieving revision 1.5
diff -u -r1.5 exception.c
--- sys/platform/vkernel/i386/exception.c	15 Jan 2007 05:27:29 -0000	1.5
+++ sys/platform/vkernel/i386/exception.c	10 Jun 2007 22:49:31 -0000
@@ -39,6 +39,7 @@
 #include <sys/types.h>
 #include <sys/systm.h>
 #include <sys/kernel.h>
+#include <sys/reboot.h>
 #include <ddb/ddb.h>
 
 #include <sys/thread2.h>
@@ -52,6 +53,7 @@
 int _udatasel = LSEL(LUDATA_SEL, SEL_UPL);
 
 static void exc_segfault(int signo, siginfo_t *info, void *ctx);
+void exc_shutdown( void );
 #ifdef DDB
 static void exc_debugger(int signo, siginfo_t *info, void *ctx);
 #endif
@@ -67,10 +69,17 @@
 	sigemptyset(&sa.sa_mask);
 	sigaction(SIGSEGV, &sa, NULL);
 	sigaction(SIGTRAP, &sa, NULL);
+
 #ifdef DDB
 	sa.sa_sigaction = exc_debugger;
 	sigaction(SIGQUIT, &sa, NULL);
 #endif
+
+	bzero(&sa, sizeof(sa));
+	sigemptyset(&sa.sa_mask);
+	sa.sa_flags |= SA_MAILBOX | SA_NODEFER;
+	sa.sa_mailbox = &mdcpu->gd_shutdown;
+	sigaction(SIGTERM, &sa, NULL);
 }
 
 /*
@@ -95,6 +104,26 @@
 	splz();
 }
 
+/*
+ * This function handles external signals for shutdown. 
+ *
+ * Currently, when a vkernel recieves a SIGTERM, either the VKERNEL init(8) 
+ * is signaled with SIGUSR2, or simply shuts down, preventing filesystem
+ * checks from being needed upon reboot.
+ */ 
+void
+exc_shutdown( void )
+{
+	mdcpu->gd_shutdown = 0;
+	kprintf("Caught SIGTERM from host system. Shutting down...\n");
+	if (initproc != NULL) {
+		ksignal(initproc, SIGUSR2);
+	}
+	else {
+		reboot(RB_POWEROFF);
+	}	
+}
+
 #ifdef DDB
 
 static void
Index: sys/platform/vkernel/i386/trap.c
===================================================================
RCS file: /var/local/apps/dcvs/src/sys/platform/vkernel/i386/trap.c,v
retrieving revision 1.23
diff -u -r1.23 trap.c
--- sys/platform/vkernel/i386/trap.c	29 Apr 2007 18:25:38 -0000	1.23
+++ sys/platform/vkernel/i386/trap.c	10 Jun 2007 22:43:41 -0000
@@ -1422,6 +1422,13 @@
 	 * user_trap() when we break out of it (usually due to a signal).
 	 */
 	for (;;) {
+
+		/*
+		 * Handle shutdown requests before attempting anything
+		 */ 
+		if (mdcpu->gd_shutdown)
+			exc_shutdown();
+
 		/*
 		 * Tell the real kernel whether it is ok to use the FP
 		 * unit or not.
Index: sys/platform/vkernel/include/globaldata.h
===================================================================
RCS file: /var/local/apps/dcvs/src/sys/platform/vkernel/include/globaldata.h,v
retrieving revision 1.5
diff -u -r1.5 globaldata.h
--- sys/platform/vkernel/include/globaldata.h	14 Jan 2007 07:59:06 -0000	1.5
+++ sys/platform/vkernel/include/globaldata.h	10 Jun 2007 21:10:26 -0000
@@ -83,7 +83,8 @@
 	int		gd_spending;	/* software interrupt pending */
 	int		gd_sdelayed;	/* delayed software ints */
 	int		gd_currentldt;
-	int		gd_mailbox;	/* signal delivery mailbox */
+	int		gd_mailbox;	/* I/O signal delivery mailbox */
+	int		gd_shutdown;    /* Shutdown signal delivery mailbox */
 	u_int		unused001;
 	u_int		gd_other_cpus;
 	u_int		gd_ss_eflags;




More information about the Submit mailing list