Move reboot() and power_off() to new file, and rename to fit convention.
[pintos-anon] / src / devices / shutdown.c
1 #include "devices/shutdown.h"
2 #include <console.h>
3 #include <stdio.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"
9 #ifdef USERPROG
10 #include "userprog/exception.h"
11 #endif
12 #ifdef FILESYS
13 #include "devices/disk.h"
14 #include "filesys/filesys.h"
15 #endif
16
17 /* Keyboard control register port. */
18 #define CONTROL_REG 0x64
19
20 static void print_stats (void);
21
22 /* Reboots the machine via the keyboard controller. */
23 void
24 shutdown_reboot (void)
25 {
26   int i;
27
28   printf ("Rebooting...\n");
29
30     /* See [kbd] for details on how to program the keyboard
31      * controller. */
32   for (i = 0; i < 100; i++)
33     {
34       int j;
35
36       /* Poll keyboard controller's status byte until
37        * 'input buffer empty' is reported. */
38       for (j = 0; j < 0x10000; j++)
39         {
40           if ((inb (CONTROL_REG) & 0x02) == 0)
41             break;
42           timer_udelay (2);
43         }
44
45       timer_udelay (50);
46
47       /* Pulse bit 0 of the output port P2 of the keyboard controller.
48        * This will reset the CPU. */
49       outb (CONTROL_REG, 0xfe);
50       timer_udelay (50);
51     }
52 }
53
54 /* Powers down the machine we're running on,
55    as long as we're running on Bochs or QEMU. */
56 void
57 shutdown_power_off (void)
58 {
59   const char s[] = "Shutdown";
60   const char *p;
61
62 #ifdef FILESYS
63   filesys_done ();
64 #endif
65
66   print_stats ();
67
68   printf ("Powering off...\n");
69   serial_flush ();
70
71   /* This is a special power-off sequence supported by Bochs and
72      QEMU, but not by physical hardware. */
73   for (p = s; *p != '\0'; p++)
74     outb (0x8900, *p);
75
76   /* This will power off a VMware VM if "gui.exitOnCLIHLT = TRUE"
77      is set in its configuration file.  (The "pintos" script does
78      that automatically.)  */
79   asm volatile ("cli; hlt" : : : "memory");
80
81   /* None of those worked. */
82   printf ("still running...\n");
83   for (;;);
84 }
85
86 /* Print statistics about Pintos execution. */
87 static void
88 print_stats (void)
89 {
90   timer_print_stats ();
91   thread_print_stats ();
92 #ifdef FILESYS
93   disk_print_stats ();
94 #endif
95   console_print_stats ();
96   kbd_print_stats ();
97 #ifdef USERPROG
98   exception_print_stats ();
99 #endif
100 }