From 64d191a88235f60a4a656eab40f844529815a8cf Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Fri, 12 Aug 2011 15:13:18 +0200 Subject: [PATCH] x86 now has a driver for rebooting. --- sortix/x86/x86.cpp | 35 +++++++++++++++++++++++++++++++++++ sortix/x86/x86.h | 2 ++ 2 files changed, 37 insertions(+) diff --git a/sortix/x86/x86.cpp b/sortix/x86/x86.cpp index 5d7d5995..4abfbbc7 100644 --- a/sortix/x86/x86.cpp +++ b/sortix/x86/x86.cpp @@ -64,5 +64,40 @@ namespace Sortix asm volatile("inl %1, %0" : "=a" (Result) : "dN" (Port)); return Result; } + + void Reboot() + { + // Keyboard interface IO port: data and control. + const uint16_t KEYBOARD_INTERFACE = 0x64; + + // Keyboard IO port. + const uint16_t KEYBOARD_IO = 0x60; + + // Keyboard data is in buffer (output buffer is empty) (bit 0). + const uint8_t KEYBOARD_DATA = (1<<0); + + // User data is in buffer (command buffer is empty) (bit 1). + const uint8_t USER_DATA = (1<<1); + + // Disable interrupts. + asm volatile("cli"); + + // Clear all keyboard buffers (output and command buffers). + uint8_t byte; + do + { + byte = InPortB(KEYBOARD_INTERFACE); + if ( ( byte & KEYBOARD_DATA ) != 0 ) { InPortB(KEYBOARD_IO); } + } while ( ( byte & USER_DATA ) != 0 ); + + // CPU reset command. + uint8_t KEYBOARD_RESET_CPU = 0xFE; + + // Now pulse the CPU reset line and reset. + OutPortB(KEYBOARD_INTERFACE, KEYBOARD_RESET_CPU); + + // If that didn't work, just halt. + asm volatile("hlt"); + } } } diff --git a/sortix/x86/x86.h b/sortix/x86/x86.h index 573767c8..4604f4da 100644 --- a/sortix/x86/x86.h +++ b/sortix/x86/x86.h @@ -44,6 +44,8 @@ namespace Sortix uint32_t int_no, err_code; // Interrupt number and error code (if applicable) uint32_t eip, cs, eflags, useresp, ss; // Pushed by the processor automatically. }; + + void Reboot(); } }