From 2cfadf3b41fd25e30e3d9879ac94f021371f0f1a Mon Sep 17 00:00:00 2001 From: David Welch Date: Mon, 16 Apr 2001 23:29:55 +0000 Subject: [PATCH] per processor TSS svn path=/trunk/; revision=1807 --- reactos/ntoskrnl/Makefile.i386 | 3 +- reactos/ntoskrnl/hal/x86/mp.c | 10 +- reactos/ntoskrnl/include/internal/ke.h | 9 ++ reactos/ntoskrnl/include/internal/ps.h | 30 ++-- reactos/ntoskrnl/ke/i386/exp.c | 99 +++--------- reactos/ntoskrnl/ke/i386/gdt.c | 53 ++++++- reactos/ntoskrnl/ke/i386/kernel.c | 42 ++++- reactos/ntoskrnl/ke/i386/ldt.c | 46 ++++-- reactos/ntoskrnl/ke/i386/multiboot.S | 8 +- reactos/ntoskrnl/ke/i386/thread.c | 9 -- reactos/ntoskrnl/ke/i386/tskswitch.S | 5 +- reactos/ntoskrnl/ke/i386/tss.c | 209 +++++++++++++++++++++++++ reactos/ntoskrnl/ke/i386/v86m_sup.S | 4 +- reactos/ntoskrnl/ke/main.c | 3 +- reactos/ntoskrnl/mm/i386/page.c | 22 +-- 15 files changed, 406 insertions(+), 146 deletions(-) create mode 100644 reactos/ntoskrnl/ke/i386/tss.c diff --git a/reactos/ntoskrnl/Makefile.i386 b/reactos/ntoskrnl/Makefile.i386 index bee30e9cf8a..ff7457e1915 100644 --- a/reactos/ntoskrnl/Makefile.i386 +++ b/reactos/ntoskrnl/Makefile.i386 @@ -26,7 +26,8 @@ OBJECTS_KE_I386 := \ ke/i386/ldt.o \ ke/i386/brkpoint.o \ ke/i386/kernel.o \ - ke/i386/fpu.o + ke/i386/fpu.o \ + ke/i386/tss.o OBJECTS_MM_I386 := \ mm/i386/memsafe.o \ diff --git a/reactos/ntoskrnl/hal/x86/mp.c b/reactos/ntoskrnl/hal/x86/mp.c index 0ce06662336..5cd0e615b79 100644 --- a/reactos/ntoskrnl/hal/x86/mp.c +++ b/reactos/ntoskrnl/hal/x86/mp.c @@ -1,4 +1,4 @@ -/* $Id: mp.c,v 1.9 2001/04/16 16:29:01 dwelch Exp $ +/* $Id: mp.c,v 1.10 2001/04/16 23:29:54 dwelch Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -43,6 +43,7 @@ typedef struct __attribute__((packed)) _COMMON_AREA_INFO ULONG Debug[16]; /* For debugging */ } COMMON_AREA_INFO, *PCOMMON_AREA_INFO; +extern PVOID Ki386InitialStackArray[MAXIMUM_PROCESSORS]; CPU_INFO CPUMap[MAX_CPU]; /* Map of all CPUs in the system */ ULONG CPUCount; /* Total number of CPUs */ @@ -1636,6 +1637,7 @@ HalInitializeProcessor ( /* Write the location of the AP stack */ Common->Stack = (ULONG)ExAllocatePool(NonPagedPool, MM_STACK_SIZE) + MM_STACK_SIZE; + Ki386InitialStackArray[CPU] = (PVOID)(Common->Stack - MM_STACK_SIZE); DPRINT("CPU %d got stack at 0x%X\n", CPU, Common->Stack); #if 0 @@ -2020,9 +2022,8 @@ HaliReadMPConfigTable( { PUCHAR pc = (PUCHAR)&Table->Signature; - DbgPrint("Bad MP configuration block signature: %c%c%c%c/%x/%x\n", - pc[0], pc[1], pc[2], pc[3], MPC_SIGNATURE, - (ULONG)Table->Signature); + DbgPrint("Bad MP configuration block signature: %c%c%c%c\n", + pc[0], pc[1], pc[2], pc[3]); KeBugCheck(0); return; } @@ -2054,7 +2055,6 @@ HaliReadMPConfigTable( Count = 0; while (Count < (Table->Length - sizeof(MP_CONFIGURATION_TABLE))) { - DbgPrint("Scanning entry %x/%x Count %x\n", Entry, *Entry, Count); /* Switch on type */ switch (*Entry) { diff --git a/reactos/ntoskrnl/include/internal/ke.h b/reactos/ntoskrnl/include/internal/ke.h index d2c6fc2f619..c38b8455c09 100644 --- a/reactos/ntoskrnl/include/internal/ke.h +++ b/reactos/ntoskrnl/include/internal/ke.h @@ -190,6 +190,15 @@ KiDispatchException(PEXCEPTION_RECORD Er, BOOLEAN SearchFrames); VOID KeTrapFrameToContext(PKTRAP_FRAME TrapFrame, PCONTEXT Context); +struct _KPCR; +VOID +KiInitializeGdt(struct _KPCR* Pcr); +VOID +KeApplicationProcessorInit(); +VOID +Ki386ApplicationProcessorInitializeTSS(VOID); +VOID +Ki386BootInitializeTSS(VOID); #endif /* not __ASM__ */ diff --git a/reactos/ntoskrnl/include/internal/ps.h b/reactos/ntoskrnl/include/internal/ps.h index 2604c2a9f67..704ca814182 100644 --- a/reactos/ntoskrnl/include/internal/ps.h +++ b/reactos/ntoskrnl/include/internal/ps.h @@ -44,6 +44,7 @@ #define KPCR_EXCEPTION_LIST 0x0 #define KPCR_SELF 0x18 +#define KPCR_TSS 0x28 #define KPCR_CURRENT_THREAD 0x124 #ifndef __ASM__ @@ -62,20 +63,21 @@ struct _KTRAPFRAME; */ typedef struct _KPCR { - PVOID ExceptionList; /* 00 */ - PVOID StackBase; /* 04 */ - PVOID StackLimit; /* 08 */ - PVOID SubSystemTib; /* 0C */ - PVOID Reserved1; /* 10 */ - PVOID ArbitraryUserPointer; /* 14 */ - struct _KPCR* Self; /* 18 */ - UCHAR ProcessorNumber; /* 1C */ - KIRQL Irql; /* 1D */ - UCHAR Reserved2[0x2]; /* 1E */ - PUSHORT IDT; /* 20 */ - PUSHORT GDT; /* 24 */ - UCHAR Reserved3[0xFC]; /* 28 */ - struct _KTHREAD* CurrentThread; /* 124 */ + PVOID ExceptionList; /* 00 */ + PVOID StackBase; /* 04 */ + PVOID StackLimit; /* 08 */ + PVOID SubSystemTib; /* 0C */ + PVOID Reserved1; /* 10 */ + PVOID ArbitraryUserPointer; /* 14 */ + struct _KPCR* Self; /* 18 */ + UCHAR ProcessorNumber; /* 1C */ + KIRQL Irql; /* 1D */ + UCHAR Reserved2[0x2]; /* 1E */ + PUSHORT IDT; /* 20 */ + PUSHORT GDT; /* 24 */ + KTSS* TSS; /* 28 */ + UCHAR Reserved3[0xF8]; /* 2C */ + struct _KTHREAD* CurrentThread; /* 124 */ } __attribute__((packed)) KPCR, *PKPCR; static inline PKPCR KeGetCurrentKPCR(VOID) diff --git a/reactos/ntoskrnl/ke/i386/exp.c b/reactos/ntoskrnl/ke/i386/exp.c index e63e3b83c86..348fa0e554c 100644 --- a/reactos/ntoskrnl/ke/i386/exp.c +++ b/reactos/ntoskrnl/ke/i386/exp.c @@ -339,11 +339,11 @@ KiDoubleFaultHandler(VOID) ULONG StackLimit; ULONG Esp0; ULONG ExceptionNr = 8; - extern KTSS KiTss; - + KTSS* OldTss; /* Use the address of the trap frame as approximation to the ring0 esp */ - Esp0 = KiTss.Esp0; + OldTss = KeGetCurrentKPCR()->TSS; + Esp0 = OldTss->Esp0; /* Get CR2 */ __asm__("movl %%cr2,%0\n\t" : "=d" (cr2)); @@ -371,10 +371,10 @@ KiDoubleFaultHandler(VOID) { DbgPrint("Exception: %d(%x)\n", ExceptionNr, 0); } - DbgPrint("CS:EIP %x:%x ", KiTss.Cs, KiTss.Eip); - print_address((PVOID)KiTss.Eip); + DbgPrint("CS:EIP %x:%x ", OldTss->Cs, OldTss->Eip); + print_address((PVOID)OldTss->Eip); DbgPrint("\n"); - DbgPrint("cr2 %x cr3 %x ", cr2, KiTss.Cr3); + DbgPrint("cr2 %x cr3 %x ", cr2, OldTss->Cr3); DbgPrint("Proc: %x ",PsGetCurrentProcess()); if (PsGetCurrentProcess() != NULL) { @@ -388,14 +388,14 @@ KiDoubleFaultHandler(VOID) PsGetCurrentThread()->Cid.UniqueThread); } DbgPrint("\n"); - DbgPrint("DS %x ES %x FS %x GS %x\n", KiTss.Ds, KiTss.Es, - KiTss.Fs, KiTss.Gs); - DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", KiTss.Eax, KiTss.Ebx, - KiTss.Ecx); - DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n", KiTss.Edx, KiTss.Ebp, - KiTss.Esi); - DbgPrint("EDI: %.8x EFLAGS: %.8x ", KiTss.Edi, KiTss.Eflags); - if (KiTss.Cs == KERNEL_CS) + DbgPrint("DS %x ES %x FS %x GS %x\n", OldTss->Ds, OldTss->Es, + OldTss->Fs, OldTss->Gs); + DbgPrint("EAX: %.8x EBX: %.8x ECX: %.8x\n", OldTss->Eax, OldTss->Ebx, + OldTss->Ecx); + DbgPrint("EDX: %.8x EBP: %.8x ESI: %.8x\n", OldTss->Edx, OldTss->Ebp, + OldTss->Esi); + DbgPrint("EDI: %.8x EFLAGS: %.8x ", OldTss->Edi, OldTss->Eflags); + if (OldTss->Cs == KERNEL_CS) { DbgPrint("kESP %.8x ", Esp0); if (PsGetCurrentThread() != NULL) @@ -407,9 +407,9 @@ KiDoubleFaultHandler(VOID) } else { - DbgPrint("User ESP %.8x\n", KiTss.Esp); + DbgPrint("User ESP %.8x\n", OldTss->Esp); } - if ((KiTss.Cs & 0xffff) == KERNEL_CS) + if ((OldTss->Cs & 0xffff) == KERNEL_CS) { DbgPrint("ESP %x\n", Esp0); stack = (PULONG) (Esp0 + 24); @@ -528,7 +528,8 @@ KiTrapHandler(PKTRAP_FRAME Tf, ULONG ExceptionNr) { DbgPrint("Exception: %d(%x)\n", ExceptionNr, Tf->ErrorCode&0xffff); } - DbgPrint("CS:EIP %x:%x ", Tf->Cs&0xffff, Tf->Eip); + DbgPrint("Processor: %d CS:EIP %x:%x ", KeGetCurrentProcessorNumber(), + Tf->Cs&0xffff, Tf->Eip); print_address((PVOID)Tf->Eip); DbgPrint("\n"); __asm__("movl %%cr3,%0\n\t" : "=d" (cr3)); @@ -665,15 +666,9 @@ void KeInitExceptions(void) int i; ULONG base, length; extern USHORT KiBootGdt[]; - extern unsigned int trap_stack_top; - extern KTSS KiTss; - extern KTSS KiTrapTss; - ULONG cr3; DPRINT("KeInitExceptions()\n",0); - __asm__("movl %%cr3,%0\n\t" : "=d" (cr3)); - /* * Set up an a descriptor for the LDT */ @@ -687,64 +682,6 @@ void KeInitExceptions(void) KiBootGdt[(LDT_SELECTOR / 2) + 3] = ((length & 0xF0000) >> 16) | ((base & 0xFF000000) >> 16); - /* - * Set up a descriptor for the TSS - */ - memset(&KiTss, 0, sizeof(KiTss)); - base = (unsigned int)&KiTss; - length = sizeof(KiTss) - 1; - - KiBootGdt[(TSS_SELECTOR / 2) + 0] = (length & 0xFFFF); - KiBootGdt[(TSS_SELECTOR / 2) + 1] = (base & 0xFFFF); - KiBootGdt[(TSS_SELECTOR / 2) + 2] = ((base & 0xFF0000) >> 16) | 0x8900; - KiBootGdt[(TSS_SELECTOR / 2) + 3] = ((length & 0xF0000) >> 16) | - ((base & 0xFF000000) >> 16); - - /* - * Initialize the TSS - */ - KiTss.Esp0 = (ULONG)&init_stack_top; - KiTss.Ss0 = KERNEL_DS; - // KiTss.IoMapBase = FIELD_OFFSET(KTSS, IoBitmap); - KiTss.IoMapBase = 0xFFFF; /* No i/o bitmap */ - KiTss.IoBitmap[0] = 0xFF; - KiTss.Ldt = LDT_SELECTOR; - - /* - * Load the task register - */ - __asm__("ltr %%ax" - : /* no output */ - : "a" (TSS_SELECTOR)); - - /* - * Set up the TSS for handling double faults - */ - memset(&KiTrapTss, 0, sizeof(KiTrapTss)); - base = (unsigned int)&KiTrapTss; - length = sizeof(KiTrapTss) - 1; - - KiBootGdt[(TRAP_TSS_SELECTOR / 2) + 0] = (length & 0xFFFF); - KiBootGdt[(TRAP_TSS_SELECTOR / 2) + 1] = (base & 0xFFFF); - KiBootGdt[(TRAP_TSS_SELECTOR / 2) + 2] = ((base & 0xFF0000) >> 16) | 0x8900; - KiBootGdt[(TRAP_TSS_SELECTOR / 2) + 3] = ((length & 0xF0000) >> 16) | - ((base & 0xFF000000) >> 16); - - KiTrapTss.Eflags = 0; - KiTrapTss.Esp0 = (ULONG)&trap_stack_top; - KiTrapTss.Ss0 = KERNEL_DS; - KiTrapTss.Esp = (ULONG)&trap_stack_top; - KiTrapTss.Cs = KERNEL_CS; - KiTrapTss.Eip = (ULONG)KiTrap8; - KiTrapTss.Ss = KERNEL_DS; - KiTrapTss.Ds = KERNEL_DS; - KiTrapTss.Es = KERNEL_DS; - KiTrapTss.Fs = PCR_SELECTOR; - KiTrapTss.IoMapBase = 0xFFFF; /* No i/o bitmap */ - KiTrapTss.IoBitmap[0] = 0xFF; - KiTrapTss.Ldt = LDT_SELECTOR; - KiTrapTss.Cr3 = cr3; - /* * Set up the other gates */ diff --git a/reactos/ntoskrnl/ke/i386/gdt.c b/reactos/ntoskrnl/ke/i386/gdt.c index edba6791b84..ff4bbbb7951 100644 --- a/reactos/ntoskrnl/ke/i386/gdt.c +++ b/reactos/ntoskrnl/ke/i386/gdt.c @@ -30,6 +30,7 @@ #include #include #include +#include #define NDEBUG #include @@ -64,11 +65,18 @@ static KSPIN_LOCK GdtLock; /* FUNCTIONS *****************************************************************/ VOID -KiInitializeGdt(ULONG Id, PKPCR Pcr) +KiInitializeGdt(PKPCR Pcr) { PUSHORT Gdt; + struct + { + USHORT Length; + ULONG Base; + } __attribute__((packed)) Descriptor; + ULONG Entry; + ULONG Base; - if (Id == 0) + if (Pcr == NULL) { KiGdtArray[0] = KiBootGdt; return; @@ -89,8 +97,45 @@ KiInitializeGdt(ULONG Id, PKPCR Pcr) * We will be initializing these later so their current values are * irrelevant. */ - memcpy(Gdt, KiGdtArray, sizeof(USHORT) * 4 * 11); - KiGdtArray[Id] = Gdt; + memcpy(Gdt, KiBootGdt, sizeof(USHORT) * 4 * 11); + KiGdtArray[Pcr->ProcessorNumber] = Gdt; + Pcr->GDT = Gdt; + + /* + * Set the base address of the PCR + */ + Base = (ULONG)Pcr; + Entry = PCR_SELECTOR / 2; + Gdt[Entry + 1] = ((ULONG)Base) & 0xffff; + + Gdt[Entry + 2] = Gdt[Entry + 2] & ~(0xff); + Gdt[Entry + 2] = Gdt[Entry + 2] | ((((ULONG)Base) & 0xff0000) >> 16); + + Gdt[Entry + 3] = Gdt[Entry + 3] & ~(0xff00); + Gdt[Entry + 3] = Gdt[Entry + 3] | ((((ULONG)Base) & 0xff000000) >> 16); + + /* + * Load the GDT + */ + Descriptor.Length = 8 * 11; + Descriptor.Base = (ULONG)Gdt; + __asm__ ("lgdt %0\n\t" : /* no output */ : "m" (Descriptor)); + + /* + * Reload the selectors + */ + __asm__ ("movl %0, %%ds\n\t" + "movl %0, %%es\n\t" + "movl %1, %%fs\n\t" + "movl %0, %%gs\n\t" + : /* no output */ + : "a" (KERNEL_DS), "b" (PCR_SELECTOR)); + __asm__ ("pushl %0\n\t" + "pushl $.l4\n\t" + "lret\n\t" + ".l4:\n\t" + : /* no output */ + : "a" (KERNEL_CS)); } VOID diff --git a/reactos/ntoskrnl/ke/i386/kernel.c b/reactos/ntoskrnl/ke/i386/kernel.c index f93d665d220..4750a18e32e 100644 --- a/reactos/ntoskrnl/ke/i386/kernel.c +++ b/reactos/ntoskrnl/ke/i386/kernel.c @@ -18,7 +18,7 @@ */ /* * PROJECT: ReactOS kernel - * FILE: ntoskrnl/ke/kernel.c + * FILE: ntoskrnl/ke/i386/kernel.c * PURPOSE: Initializes the kernel * PROGRAMMER: David Welch (welch@mcmail.com) * UPDATE HISTORY: @@ -39,17 +39,52 @@ /* GLOBALS *******************************************************************/ ULONG KiPcrInitDone = 0; +static ULONG PcrsAllocated = 0; /* FUNCTIONS *****************************************************************/ +VOID +KeApplicationProcessorInit() +{ + PKPCR KPCR; + ULONG Offset; + + /* + * Create a PCR for this processor + */ + Offset = InterlockedIncrement(&PcrsAllocated); + KPCR = (PKPCR)(KPCR_BASE + (Offset * PAGESIZE)); + MmCreateVirtualMapping(NULL, + (PVOID)KPCR, + PAGE_READWRITE, + (ULONG)MmAllocPage(0)); + memset(KPCR, 0, PAGESIZE); + KPCR->ProcessorNumber = Offset; + KPCR->Self = KPCR; + KPCR->Irql = HIGH_LEVEL; + + /* + * Initialize the GDT + */ + KiInitializeGdt(KPCR); + + /* + * Initialize the TSS + */ + Ki386ApplicationProcessorInitializeTSS(); +} + VOID KeInit1(VOID) { PKPCR KPCR; extern USHORT KiBootGdt[]; + extern KTSS KiBootTss; KiCheckFPU(); - + + KiInitializeGdt (NULL); + Ki386BootInitializeTSS(); KeInitExceptions (); KeInitInterrupts (); @@ -64,7 +99,10 @@ KeInit1(VOID) KPCR->Irql = HIGH_LEVEL; KPCR->GDT = (PUSHORT)&KiBootGdt; KPCR->IDT = (PUSHORT)&KiIdt; + KPCR->TSS = &KiBootTss; + KPCR->ProcessorNumber = 0; KiPcrInitDone = 1; + PcrsAllocated++; } VOID diff --git a/reactos/ntoskrnl/ke/i386/ldt.c b/reactos/ntoskrnl/ke/i386/ldt.c index a689760dbdd..c269e501d25 100644 --- a/reactos/ntoskrnl/ke/i386/ldt.c +++ b/reactos/ntoskrnl/ke/i386/ldt.c @@ -1,9 +1,26 @@ /* - * COPYRIGHT: See COPYING in the top level directory + * ReactOS kernel + * Copyright (C) 2000 ReactOS Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/* * PROJECT: ReactOS kernel - * FILE: ntoskrnl/ke/ldt.c + * FILE: ntoskrnl/ke/i386/ldt.c * PURPOSE: LDT managment - * PROGRAMMER: David Welch (welch@mcmail.com) + * PROGRAMMER: David Welch (welch@cwcom.net) * UPDATE HISTORY: * Created 22/05/98 */ @@ -11,18 +28,27 @@ /* INCLUDES *****************************************************************/ #include +#include +#include +#include +#define NDEBUG #include +/* GLOBALS *******************************************************************/ + +/* + * + */ +//STATIC UCHAR KiNullLdt[8] = {0,}; + /* FUNCTIONS *****************************************************************/ -NTSTATUS STDCALL -NtSetLdtEntries(HANDLE Thread, ULONG FirstEntry, PULONG Entries) +NTSTATUS STDCALL +NtSetLdtEntries (HANDLE Thread, + ULONG FirstEntry, + PULONG Entries) { - UNIMPLEMENTED; + return(STATUS_NOT_IMPLEMENTED); } - - - - diff --git a/reactos/ntoskrnl/ke/i386/multiboot.S b/reactos/ntoskrnl/ke/i386/multiboot.S index ff97209676c..6791d8f5c2c 100644 --- a/reactos/ntoskrnl/ke/i386/multiboot.S +++ b/reactos/ntoskrnl/ke/i386/multiboot.S @@ -197,7 +197,7 @@ _multiboot_entry: * Load the GDTR and IDTR with new tables located above * 0xc0000000 */ - + /* FIXME: Application processors should have their own GDT/IDT */ lgdt _KiGdtDescriptor lidt _KiIdtDescriptor @@ -236,12 +236,12 @@ _multiboot_entry: pushl $KERNEL_CS pushl $_KiSystemStartup lret - popl %eax /* * Catch illegal returns from KiSystemStartup */ .l7: + popl %eax pushl $0 call _KeBugCheck@4 popl %eax @@ -274,14 +274,14 @@ _multiboot_entry: pushl $KERNEL_CS pushl $__main lret - popl %eax - popl %eax /* * Catch illegal returns from main, try bug checking the system, * if that fails then loop forever. */ .l5: + popl %eax + popl %eax pushl $0 call _KeBugCheck@4 popl %eax diff --git a/reactos/ntoskrnl/ke/i386/thread.c b/reactos/ntoskrnl/ke/i386/thread.c index 6cc2e49a900..e897ee72967 100644 --- a/reactos/ntoskrnl/ke/i386/thread.c +++ b/reactos/ntoskrnl/ke/i386/thread.c @@ -37,15 +37,6 @@ #define NDEBUG #include -/* GLOBALS ***************************************************************/ - -KTSS KiTss; -KTSS KiTrapTss; - -extern USHORT KiGdt[]; - -extern unsigned int init_stack_top; - /* FUNCTIONS **************************************************************/ #define FLAG_NT (1<<14) diff --git a/reactos/ntoskrnl/ke/i386/tskswitch.S b/reactos/ntoskrnl/ke/i386/tskswitch.S index 4842d63c7fc..b4a7057f424 100644 --- a/reactos/ntoskrnl/ke/i386/tskswitch.S +++ b/reactos/ntoskrnl/ke/i386/tskswitch.S @@ -80,7 +80,7 @@ _Ki386ContextSwitch: /* * Set the current thread information in the PCR */ - movl %ebx, %fs:KPCR_CURRENT_THREAD + movl %ebx, %fs:KPCR_CURRENT_THREAD /* * FIXME: Save debugging state. @@ -102,7 +102,8 @@ _Ki386ContextSwitch: * Set the stack pointer in this processors TSS */ movl KTHREAD_INITIAL_STACK(%ebx), %eax - movl %eax, (_KiTss + KTSS_ESP0) + movl %fs:KPCR_TSS, %esi + movl %eax, KTSS_ESP0(%esi) /* * Change the address space diff --git a/reactos/ntoskrnl/ke/i386/tss.c b/reactos/ntoskrnl/ke/i386/tss.c new file mode 100644 index 00000000000..ca0279670f4 --- /dev/null +++ b/reactos/ntoskrnl/ke/i386/tss.c @@ -0,0 +1,209 @@ +/* + * ReactOS kernel + * Copyright (C) 2000 ReactOS Team + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + */ +/* + * PROJECT: ReactOS kernel + * FILE: ntoskrnl/ke/i386/tss.c + * PURPOSE: TSS managment + * PROGRAMMER: David Welch (welch@cwcom.net) + * UPDATE HISTORY: + * Created 22/05/98 + */ + +/* INCLUDES *****************************************************************/ + +#include +#include +#include +#include + +#define NDEBUG +#include + +/* GLOBALS *******************************************************************/ + +static KTSS* Ki386TssArray[MAXIMUM_PROCESSORS]; +PVOID Ki386InitialStackArray[MAXIMUM_PROCESSORS]; +static KTSS* Ki386TrapTssArray[MAXIMUM_PROCESSORS]; +static PVOID Ki386TrapStackArray[MAXIMUM_PROCESSORS]; + +KTSS KiBootTss; +static KTSS KiBootTrapTss; + +extern USHORT KiBootGdt[]; + +extern VOID KiTrap8(VOID); + +/* FUNCTIONS *****************************************************************/ + +VOID +Ki386ApplicationProcessorInitializeTSS(VOID) +{ + ULONG cr3; + KTSS* Tss; + KTSS* TrapTss; + PVOID TrapStack; + ULONG Id; + PUSHORT Gdt; + ULONG base, length; + + Id = KeGetCurrentProcessorNumber(); + Gdt = KeGetCurrentKPCR()->GDT; + + __asm__("movl %%cr3,%0\n\t" : "=d" (cr3)); + + Tss = ExAllocatePool(NonPagedPool, sizeof(KTSS)); + TrapTss = ExAllocatePool(NonPagedPool, sizeof(KTSS)); + TrapStack = ExAllocatePool(NonPagedPool, MM_STACK_SIZE); + + Ki386TssArray[Id] = Tss; + Ki386TrapTssArray[Id] = TrapTss; + Ki386TrapStackArray[Id] = TrapStack; + KeGetCurrentKPCR()->TSS = Tss; + + /* Initialize the boot TSS. */ + Tss->Esp0 = (ULONG)Ki386InitialStackArray[Id]; + Tss->Ss0 = KERNEL_DS; + // Tss.IoMapBase = FIELD_OFFSET(KTSS, IoBitmap); + Tss->IoMapBase = 0xFFFF; /* No i/o bitmap */ + Tss->IoBitmap[0] = 0xFF; + Tss->Ldt = LDT_SELECTOR; + + /* + * Initialize a descriptor for the TSS + */ + base = (ULONG)Tss; + length = sizeof(KTSS) - 1; + + Gdt[(TSS_SELECTOR / 2) + 0] = (length & 0xFFFF); + Gdt[(TSS_SELECTOR / 2) + 1] = (base & 0xFFFF); + Gdt[(TSS_SELECTOR / 2) + 2] = ((base & 0xFF0000) >> 16) | 0x8900; + Gdt[(TSS_SELECTOR / 2) + 3] = ((length & 0xF0000) >> 16) | + ((base & 0xFF000000) >> 16); + + /* Initialize the TSS used for handling double faults. */ + TrapTss->Eflags = 0; + TrapTss->Esp0 = ((ULONG)TrapStack + MM_STACK_SIZE); + TrapTss->Ss0 = KERNEL_DS; + TrapTss->Esp = ((ULONG)TrapStack + MM_STACK_SIZE); + TrapTss->Cs = KERNEL_CS; + TrapTss->Eip = (ULONG)KiTrap8; + TrapTss->Ss = KERNEL_DS; + TrapTss->Ds = KERNEL_DS; + TrapTss->Es = KERNEL_DS; + TrapTss->Fs = PCR_SELECTOR; + TrapTss->IoMapBase = 0xFFFF; /* No i/o bitmap */ + TrapTss->IoBitmap[0] = 0xFF; + TrapTss->Ldt = LDT_SELECTOR; + TrapTss->Cr3 = cr3; + + /* + * Initialize a descriptor for the trap TSS. + */ + base = (ULONG)TrapTss; + length = sizeof(KTSS) - 1; + + Gdt[(TRAP_TSS_SELECTOR / 2) + 0] = (length & 0xFFFF); + Gdt[(TRAP_TSS_SELECTOR / 2) + 1] = (base & 0xFFFF); + Gdt[(TRAP_TSS_SELECTOR / 2) + 2] = ((base & 0xFF0000) >> 16) | 0x8900; + Gdt[(TRAP_TSS_SELECTOR / 2) + 3] = ((length & 0xF0000) >> 16) | + ((base & 0xFF000000) >> 16); + + /* + * Load the task register + */ + __asm__("ltr %%ax" + : /* no output */ + : "a" (TSS_SELECTOR)); +} + +VOID +Ki386BootInitializeTSS(VOID) +{ + ULONG cr3; + extern unsigned int init_stack, init_stack_top; + extern unsigned int trap_stack, trap_stack_top; + unsigned int base, length; + + __asm__("movl %%cr3,%0\n\t" : "=d" (cr3)); + + Ki386TssArray[0] = &KiBootTss; + Ki386TrapTssArray[0] = &KiBootTrapTss; + Ki386TrapStackArray[0] = (PVOID)&trap_stack; + Ki386InitialStackArray[0] = (PVOID)&init_stack; + + /* Initialize the boot TSS. */ + KiBootTss.Esp0 = (ULONG)&init_stack_top; + KiBootTss.Ss0 = KERNEL_DS; + // KiBootTss.IoMapBase = FIELD_OFFSET(KTSS, IoBitmap); + KiBootTss.IoMapBase = 0xFFFF; /* No i/o bitmap */ + KiBootTss.IoBitmap[0] = 0xFF; + KiBootTss.Ldt = LDT_SELECTOR; + + /* + * Initialize a descriptor for the TSS + */ + base = (unsigned int)&KiBootTss; + length = sizeof(KiBootTss) - 1; + + KiBootGdt[(TSS_SELECTOR / 2) + 0] = (length & 0xFFFF); + KiBootGdt[(TSS_SELECTOR / 2) + 1] = (base & 0xFFFF); + KiBootGdt[(TSS_SELECTOR / 2) + 2] = ((base & 0xFF0000) >> 16) | 0x8900; + KiBootGdt[(TSS_SELECTOR / 2) + 3] = ((length & 0xF0000) >> 16) | + ((base & 0xFF000000) >> 16); + + /* Initialize the TSS used for handling double faults. */ + KiBootTrapTss.Eflags = 0; + KiBootTrapTss.Esp0 = (ULONG)&trap_stack_top; + KiBootTrapTss.Ss0 = KERNEL_DS; + KiBootTrapTss.Esp = (ULONG)&trap_stack_top; + KiBootTrapTss.Cs = KERNEL_CS; + KiBootTrapTss.Eip = (ULONG)KiTrap8; + KiBootTrapTss.Ss = KERNEL_DS; + KiBootTrapTss.Ds = KERNEL_DS; + KiBootTrapTss.Es = KERNEL_DS; + KiBootTrapTss.Fs = PCR_SELECTOR; + KiBootTrapTss.IoMapBase = 0xFFFF; /* No i/o bitmap */ + KiBootTrapTss.IoBitmap[0] = 0xFF; + KiBootTrapTss.Ldt = LDT_SELECTOR; + KiBootTrapTss.Cr3 = cr3; + + /* + * Initialize a descriptor for the trap TSS. + */ + base = (unsigned int)&KiBootTrapTss; + length = sizeof(KiBootTrapTss) - 1; + + KiBootGdt[(TRAP_TSS_SELECTOR / 2) + 0] = (length & 0xFFFF); + KiBootGdt[(TRAP_TSS_SELECTOR / 2) + 1] = (base & 0xFFFF); + KiBootGdt[(TRAP_TSS_SELECTOR / 2) + 2] = ((base & 0xFF0000) >> 16) | 0x8900; + KiBootGdt[(TRAP_TSS_SELECTOR / 2) + 3] = ((length & 0xF0000) >> 16) | + ((base & 0xFF000000) >> 16); + + /* + * Load the task register + */ + __asm__("ltr %%ax" + : /* no output */ + : "a" (TSS_SELECTOR)); +} + + + + + diff --git a/reactos/ntoskrnl/ke/i386/v86m_sup.S b/reactos/ntoskrnl/ke/i386/v86m_sup.S index dcfa7a3dd22..2d0a7464b7c 100644 --- a/reactos/ntoskrnl/ke/i386/v86m_sup.S +++ b/reactos/ntoskrnl/ke/i386/v86m_sup.S @@ -89,7 +89,7 @@ _Ki386RetToV86Mode: * will be the current stack adjusted so we don't overwrite the * existing stack frames */ - movl $_KiTss, %esi + movl %fs:KPCR_TSS, %esi movl %esp, KTSS_ESP0(%esi) /* @@ -181,7 +181,7 @@ _KiV86Complete: * Restore the initial stack */ popl %eax - movl $_KiTss, %esi + movl %fs:KPCR_TSS, %esi movl %eax, KTSS_ESP0(%esi) /* diff --git a/reactos/ntoskrnl/ke/main.c b/reactos/ntoskrnl/ke/main.c index 1aef1905e9a..a1e1d9510a3 100644 --- a/reactos/ntoskrnl/ke/main.c +++ b/reactos/ntoskrnl/ke/main.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: main.c,v 1.90 2001/04/16 16:29:02 dwelch Exp $ +/* $Id: main.c,v 1.91 2001/04/16 23:29:54 dwelch Exp $ * * PROJECT: ReactOS kernel * FILE: ntoskrnl/ke/main.c @@ -576,6 +576,7 @@ KiSystemStartup(BOOLEAN BootProcessor) KeBugCheck(0); } /* Do application processor initialization */ + KeApplicationProcessorInit(); for(;;); } diff --git a/reactos/ntoskrnl/mm/i386/page.c b/reactos/ntoskrnl/mm/i386/page.c index a4b811aba07..89df80cbe01 100644 --- a/reactos/ntoskrnl/mm/i386/page.c +++ b/reactos/ntoskrnl/mm/i386/page.c @@ -16,7 +16,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ -/* $Id: page.c,v 1.27 2001/04/16 02:02:05 dwelch Exp $ +/* $Id: page.c,v 1.28 2001/04/16 23:29:55 dwelch Exp $ * * PROJECT: ReactOS kernel * FILE: ntoskrnl/mm/i386/page.c @@ -661,16 +661,16 @@ MmCreateVirtualMapping(PEPROCESS Process, ULONG flProtect, ULONG PhysicalAddress) { - if (!MmIsUsablePage((PVOID)PhysicalAddress)) - { - DPRINT1("Page at address %x not usable\n", PhysicalAddress); - KeBugCheck(0); - } - - return(MmCreateVirtualMappingUnsafe(Process, - Address, - flProtect, - PhysicalAddress)); + if (!MmIsUsablePage((PVOID)PhysicalAddress)) + { + DPRINT1("Page at address %x not usable\n", PhysicalAddress); + KeBugCheck(0); + } + + return(MmCreateVirtualMappingUnsafe(Process, + Address, + flProtect, + PhysicalAddress)); } ULONG