From 3c245fd7a3b31264e8d6706347bead155ec2f0fd Mon Sep 17 00:00:00 2001 From: Timo Kreuzer Date: Wed, 15 Jun 2011 21:00:52 +0000 Subject: [PATCH] [FREELDR] - Setup the IDT in C code instead of using 16 bit assembly and relying on trap handlers being below 64k - Make the trap handler code MSVC compatible - Add back multiboot code svn path=/trunk/; revision=52256 --- reactos/boot/freeldr/freeldr/CMakeLists.txt | 13 +-- .../boot/freeldr/freeldr/arch/i386/entry.S | 18 +++- .../boot/freeldr/freeldr/arch/i386/i386idt.c | 42 +++++++++ .../boot/freeldr/freeldr/arch/i386/i386trap.S | 86 +++++++++++-------- .../freeldr/freeldr/arch/i386/multiboot.S | 39 +++++++++ 5 files changed, 155 insertions(+), 43 deletions(-) create mode 100644 reactos/boot/freeldr/freeldr/arch/i386/i386idt.c diff --git a/reactos/boot/freeldr/freeldr/CMakeLists.txt b/reactos/boot/freeldr/freeldr/CMakeLists.txt index 21ed84d2e7e..85b3d5a3671 100644 --- a/reactos/boot/freeldr/freeldr/CMakeLists.txt +++ b/reactos/boot/freeldr/freeldr/CMakeLists.txt @@ -9,22 +9,23 @@ if(ARCH MATCHES arm) endif() if(ARCH MATCHES i386) + list(APPEND FREELDR_BASE64K_SOURCE + arch/i386/entry.S + arch/i386/i386idt.c + arch/i386/i386trap.S + arch/i386/i386bug.c) if(NOT MSVC) list(APPEND FREELDR_BASE64K_SOURCE - arch/i386/entry.S arch/i386/boot.S arch/i386/drvmap.S arch/i386/i386cpu.S - arch/i386/i386idt.S arch/i386/i386pnp.S arch/i386/i386pxe.S - arch/i386/i386trap.S arch/i386/linux.S - arch/i386/mb.S - arch/i386/i386bug.c) + arch/i386/multiboot.S + arch/i386/mb.S) else() list(APPEND FREELDR_BASE64K_SOURCE - arch/i386/entry.S arch/i386/realmode.S) endif() elseif(ARCH MATCHES amd64) diff --git a/reactos/boot/freeldr/freeldr/arch/i386/entry.S b/reactos/boot/freeldr/freeldr/arch/i386/entry.S index 2c92c66106b..835cf7c11e0 100644 --- a/reactos/boot/freeldr/freeldr/arch/i386/entry.S +++ b/reactos/boot/freeldr/freeldr/arch/i386/entry.S @@ -19,10 +19,11 @@ #include #include -#include EXTERN _BootMain:PROC EXTERN _InitIdt:PROC +EXTERN _i386Idt:DWORD +//EXTERN _i386idtptr:FWORD .code32 @@ -41,7 +42,11 @@ _RealEntryPoint: mov esp, dword ptr [stack32] /* Load the IDT */ +#ifdef _USE_ML + lidt fword ptr [i386idtptr] +#else lidt i386idtptr +#endif /* Continue execution */ jmp dword ptr [ContinueAddress] @@ -63,6 +68,9 @@ _FrldrStartup: mov eax, dword ptr ds:[BSS_RealModeEntry] mov dword ptr ds:[SwitchToReal16Address], eax + /* Initialize the idt */ + call _InitIdt + /* GO! */ xor eax, eax push eax @@ -106,8 +114,9 @@ EXTERN(switch_to_prot) /* Load the GDT */ lgdt gdtptr + /* Load the IDT */ - lidt i386idtptr + lidt i386idtptr /* Enable Protected Mode */ mov eax, cr0 @@ -352,6 +361,11 @@ rmode_idtptr: .word HEX(3ff) /* Limit */ .long 0 /* Base Address */ +PUBLIC i386idtptr +i386idtptr: + .word 255 /* Limit */ + .long _i386Idt /* Base Address */ + PUBLIC _FrldrBootDrive _FrldrBootDrive: .long 0 diff --git a/reactos/boot/freeldr/freeldr/arch/i386/i386idt.c b/reactos/boot/freeldr/freeldr/arch/i386/i386idt.c new file mode 100644 index 00000000000..3444e094151 --- /dev/null +++ b/reactos/boot/freeldr/freeldr/arch/i386/i386idt.c @@ -0,0 +1,42 @@ + +#include + + +KIDTENTRY DECLSPEC_ALIGN(4) i386Idt[32]; +KDESCRIPTOR i386IdtDescriptor = {0, 255, i386Idt}; + +static +void +InitIdtVector( + UCHAR Vector, + PVOID ServiceHandler, + USHORT Access) +{ + i386Idt[Vector].Offset = (ULONG)ServiceHandler & 0xffff; + i386Idt[Vector].ExtendedOffset = (ULONG)ServiceHandler >> 16; + i386Idt[Vector].Selector = PMODE_CS; + i386Idt[Vector].Access = Access; +} + +void +InitIdt(void) +{ + InitIdtVector(0, i386DivideByZero, 0x8e00); + InitIdtVector(1, i386DebugException, 0x8e00); + InitIdtVector(2, i386NMIException, 0x8e00); + InitIdtVector(3, i386Breakpoint, 0x8e00); + InitIdtVector(4, i386Overflow, 0x8e00); + InitIdtVector(5, i386BoundException, 0x8e00); + InitIdtVector(6, i386InvalidOpcode, 0x8e00); + InitIdtVector(7, i386FPUNotAvailable, 0x8e00); + InitIdtVector(8, i386DoubleFault, 0x8e00); + InitIdtVector(9, i386CoprocessorSegment, 0x8e00); + InitIdtVector(10, i386InvalidTSS, 0x8e00); + InitIdtVector(11, i386SegmentNotPresent, 0x8e00); + InitIdtVector(12, i386StackException, 0x8e00); + InitIdtVector(13, i386GeneralProtectionFault, 0x8e00); + InitIdtVector(14, i386PageFault, 0x8e00); + InitIdtVector(16, i386CoprocessorError, 0x8e00); + InitIdtVector(17, i386AlignmentCheck, 0x8e00); + InitIdtVector(18, i386MachineCheck, 0x8e00); +} diff --git a/reactos/boot/freeldr/freeldr/arch/i386/i386trap.S b/reactos/boot/freeldr/freeldr/arch/i386/i386trap.S index 152e3863a60..c579cb96c28 100644 --- a/reactos/boot/freeldr/freeldr/arch/i386/i386trap.S +++ b/reactos/boot/freeldr/freeldr/arch/i386/i386trap.S @@ -17,16 +17,17 @@ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ -.intel_syntax noprefix - .text - .code16 - +#include #include #include #define SCREEN_ATTR 0x1f /* Bright white on blue background */ -.macro SAVE_CPU_REGS +EXTERN _i386PrintExceptionText@12:PROC + +.code32 + +MACRO(SAVE_CPU_REGS) /* push the rest of the KTRAP_FRAME */ push ebp push ebx @@ -60,8 +61,8 @@ sub esp, 44 sgdt [esp] sidt [esp + 8] - str [esp + 16] - sldt [esp + 18] + str word ptr [esp + 16] + sldt word ptr [esp + 18] mov eax, dr7; push eax mov eax, dr6; @@ -82,16 +83,14 @@ push eax mov eax, cr0; push eax -.endm +ENDM /* Set by each exception handler to the address of the description text */ i386ExceptionIndex: .long 0 - /************************************************************************/ i386CommonExceptionHandler: - .code32 SAVE_CPU_REGS @@ -108,21 +107,32 @@ i386ExceptionHandlerHang: iret -.macro TRAP_STUB function, index - EXTERN(\function) +MACRO(TRAP_STUB, function, index) + PUBLIC VAL(function) +#ifdef _USE_ML + function: +#else + \function: +#endif push 0 // Fake error code - mov dword ptr i386ExceptionIndex, \index + mov dword ptr i386ExceptionIndex, VAL(index) jmp i386CommonExceptionHandler -.endm +ENDM -.macro TRAP_STUB2 function, index - EXTERN(\function) - mov dword ptr i386ExceptionIndex, \index +MACRO(TRAP_STUB2, function, index) + PUBLIC VAL(function) +#ifdef _USE_ML + function: +#else + \function: +#endif + mov dword ptr i386ExceptionIndex, VAL(index) jmp i386CommonExceptionHandler -.endm +ENDM /************************************************************************/ TRAP_STUB _i386DivideByZero, 0 + TRAP_STUB _i386DebugException, 1 TRAP_STUB _i386NMIException, 2 TRAP_STUB _i386Breakpoint, 3 @@ -147,30 +157,36 @@ TRAP_STUB _i386SimdFloatError, 19 * DEBUGGING SUPPORT FUNCTIONS ************************************************************************/ -.macro BREAKPOINT_TEPLATE functionname, mask1, mask2 - EXTERN(\functionname) +MACRO(BREAKPOINT_TEPLATE, functionname, mask1, mask2) + PUBLIC VAL(functionname) +#ifdef _USE_ML + functionname: +#else + \functionname: +#endif push eax mov eax, [esp + 8] mov dr3, eax mov eax, dr7 - and eax, \mask1 - or eax, \mask2 + and eax, VAL(mask1) + or eax, VAL(mask2) mov dr7, eax pop eax ret -.endm +ENDM -BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT1, 0xfff0ffff, 0x00000303 -BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT1, 0xfff0ffff, 0x00030303 -BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT1, 0xfff0ffff, 0x00010303 -BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT2, 0xff0fffff, 0x0000030c -BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT2, 0xff0fffff, 0x0030030c -BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT2, 0xff0fffff, 0x0010030c -BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT3, 0xf0ffffff, 0x00000330 -BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT3, 0xf0ffffff, 0x03000330 -BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT3, 0xf0ffffff, 0x01000330 -BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT4, 0x0fffffff, 0x000003c0 -BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT4, 0x0fffffff, 0x300003c0 -BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT4, 0x0fffffff, 0x100003c0 +BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT1, HEX(0fff0ffff), HEX(000000303) +BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT1, HEX(0fff0ffff), HEX(000030303) +BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT1, HEX(0fff0ffff), HEX(000010303) +BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT2, HEX(0ff0fffff), HEX(00000030c) +BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT2, HEX(0ff0fffff), HEX(00030030c) +BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT2, HEX(0ff0fffff), HEX(00010030c) +BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT3, HEX(0f0ffffff), HEX(000000330) +BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT3, HEX(0f0ffffff), HEX(003000330) +BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT3, HEX(0f0ffffff), HEX(001000330) +BREAKPOINT_TEPLATE _INSTRUCTION_BREAKPOINT4, HEX(00fffffff), HEX(0000003c0) +BREAKPOINT_TEPLATE _MEMORY_READWRITE_BREAKPOINT4, HEX(00fffffff), HEX(0300003c0) +BREAKPOINT_TEPLATE _MEMORY_WRITE_BREAKPOINT4, HEX(00fffffff), HEX(0100003c0) +END diff --git a/reactos/boot/freeldr/freeldr/arch/i386/multiboot.S b/reactos/boot/freeldr/freeldr/arch/i386/multiboot.S index bdb32e14271..3eff7b2caa9 100644 --- a/reactos/boot/freeldr/freeldr/arch/i386/multiboot.S +++ b/reactos/boot/freeldr/freeldr/arch/i386/multiboot.S @@ -26,6 +26,8 @@ * * Allows freeldr to be loaded as a "multiboot kernel" by * other boot loaders like Grub + * This code is not referenced from anywhere. GRUB searches for + * the header signature and uses the header to load it. */ #define MB_INFO_SIZE 90 @@ -172,6 +174,43 @@ mb_info: cmdline: .fill CMDLINE_SIZE, 1, 0 +.align 4 /* force 4-byte alignment */ +gdt: + /* NULL Descriptor */ + .word HEX(0000) + .word HEX(0000) + .word HEX(0000) + .word HEX(0000) + + /* 32-bit flat CS */ + .word HEX(FFFF) + .word HEX(0000) + .word HEX(9A00) + .word HEX(00CF) + + /* 32-bit flat DS */ + .word HEX(FFFF) + .word HEX(0000) + .word HEX(9200) + .word HEX(00CF) + + /* 16-bit real mode CS */ + .word HEX(FFFF) + .word HEX(0000) + .word HEX(9E00) + .word HEX(0000) + + /* 16-bit real mode DS */ + .word HEX(FFFF) + .word HEX(0000) + .word HEX(9200) + .word HEX(0000) + +/* GDT table pointer */ +gdtptr: + .word HEX(27) /* Limit */ + .long gdt /* Base Address */ + /* Initial GDT table pointer for multiboot */ gdtptrhigh: .word HEX(27) /* Limit */