1 #include "devices/shutdown.h"
4 #include "devices/kbd.h"
5 #include "devices/serial.h"
6 #include "devices/timer.h"
7 #include "threads/io.h"
8 #include "threads/thread.h"
10 #include "userprog/exception.h"
13 #include "devices/block.h"
14 #include "filesys/filesys.h"
17 /* Keyboard control register port. */
18 #define CONTROL_REG 0x64
20 /* How to shut down when shutdown() is called. */
21 static enum shutdown_type how = SHUTDOWN_NONE;
23 static void print_stats (void);
25 /* Shuts down the machine in the way configured by
26 shutdown_configure(). If the shutdown type is SHUTDOWN_NONE
27 (which is the default), returns without doing anything. */
33 case SHUTDOWN_POWER_OFF:
34 shutdown_power_off ();
47 /* Sets TYPE as the way that machine will shut down when Pintos
48 execution is complete. */
50 shutdown_configure (enum shutdown_type type)
55 /* Reboots the machine via the keyboard controller. */
57 shutdown_reboot (void)
59 printf ("Rebooting...\n");
61 /* See [kbd] for details on how to program the keyboard
67 /* Poll keyboard controller's status byte until
68 * 'input buffer empty' is reported. */
69 for (i = 0; i < 0x10000; i++)
71 if ((inb (CONTROL_REG) & 0x02) == 0)
78 /* Pulse bit 0 of the output port P2 of the keyboard controller.
79 * This will reset the CPU. */
80 outb (CONTROL_REG, 0xfe);
85 /* Powers down the machine we're running on,
86 as long as we're running on Bochs or QEMU. */
88 shutdown_power_off (void)
90 const char s[] = "Shutdown";
99 printf ("Powering off...\n");
103 outw (0xB004, 0x2000);
105 /* This is a special power-off sequence supported by Bochs and
106 QEMU, but not by physical hardware. */
107 for (p = s; *p != '\0'; p++)
110 /* For newer versions of qemu, you must run with -device
111 * isa-debug-exit, which exits on any write to an IO port (by
112 * default 0x501). Qemu's exit code is double the value plus one,
113 * so there is no way to exit cleanly. We use 0x31 which should
114 * result in a qemu exit code of 0x63. */
117 /* This will power off a VMware VM if "gui.exitOnCLIHLT = TRUE"
118 is set in its configuration file. (The "pintos" script does
119 that automatically.) */
120 asm volatile ("cli; hlt" : : : "memory");
122 /* None of those worked. */
123 printf ("still running...\n");
127 /* Print statistics about Pintos execution. */
131 timer_print_stats ();
132 thread_print_stats ();
134 block_print_stats ();
136 console_print_stats ();
139 exception_print_stats ();