Skip to content

Commit c2da413

Browse files
author
tthomps
committed
Create a new page directory for each new task (except the GUI shell, which shares the kernel's page directory).
In KPageAllocator(), check if we're mapping a new page into kernel space, and if so, copy that mapping into all the other page directories of running tasks.
1 parent 4b79fb0 commit c2da413

File tree

9 files changed

+71
-11
lines changed

9 files changed

+71
-11
lines changed

MyOS_1/Executables/PE32.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#include "../Terminal.h"
66
#include "../printf.h"
77
#include "../Tasks/Context.h"
8-
8+
#include "../paging.h"
99

1010
jmp_buf peReturnBuf;
1111

@@ -81,6 +81,26 @@ bool loadAndRunPE(uint8_t *executableDestination, DOS_Header *mzAddress, const c
8181
}
8282
}
8383

84+
// Disable interrupts
85+
_disable();
86+
87+
// Get a new page directory for the new task
88+
PAGE_DIRECTORY_ENTRY *newPageDirectory;
89+
90+
// GUI.exe shares the kernel's page directory
91+
if ((uint32_t)executableDestination != 0xC00000)
92+
{
93+
newPageDirectory = (PAGE_DIRECTORY_ENTRY *)nextPageDirectory;
94+
nextPageDirectory += sizeof(PAGE_DIRECTORY_ENTRY) * 1024;
95+
96+
// Copy the kernel's page table to this new page table
97+
memcpy((void*)newPageDirectory, pageDir, sizeof(PAGE_DIRECTORY_ENTRY) * 1024);
98+
99+
//Paging_Print_Page_Table(newPageDirectory);
100+
}
101+
else
102+
newPageDirectory = pageDir;
103+
84104
// TEMPTEMP - zero out 5 0x1000 sections of memory (tailored to TestApp1.exe)
85105
memset(executableDestination, 0, 0x5000);
86106

@@ -140,10 +160,13 @@ bool loadAndRunPE(uint8_t *executableDestination, DOS_Header *mzAddress, const c
140160
if (multiEnable)
141161
{
142162
// TEMP: use kernel page directory
143-
DispatchNewTask((uint32_t)entryPoint, __readcr3(), 0x20000, imageName, exclusive);
163+
DispatchNewTask((uint32_t)entryPoint, newPageDirectory, 0x20000, imageName, exclusive);
144164
}
145165
else
146-
{
166+
{
167+
// Re-enable interrupts
168+
_enable();
169+
147170
/*_asm {
148171
// Setup the program's stack
149172
mov [espVal], esp

MyOS_1/Interrupts/Interrupts.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -558,7 +558,9 @@ void _declspec(naked) page_fault_handler(void)
558558

559559
terminal_fill(' ', VGA_COLOR_WHITE, VGA_COLOR_BLUE);
560560

561-
terminal_writestring("Page Fault handler fired.\nError code ");
561+
terminal_writestring("Page Fault handler fired by ");
562+
terminal_writestring(tasks[currentTask].imageName);
563+
terminal_writestring(".\nError code ");
562564
terminal_print_ulong_hex(errorCode);
563565
terminal_writestring("\nOffending instruction at ");
564566
terminal_print_ulong_hex(address);

MyOS_1/Interrupts/System_Calls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ void SystemCallPageAllocator(unsigned int pages, unsigned int *pPagesAllocated,
187187
push pReturnVal
188188
push pPagesAllocated
189189
push pages
190-
int SYSCALL_PAGE_ALLOCATOR // call PageAllocator(pagesToAllocate, pPagesAllocated, pReturnVal);
190+
int SYSCALL_PAGE_ALLOCATOR // call page_allocator_interrupt_handler(pagesToAllocate, pPagesAllocated, pReturnVal);
191191
add esp, pointerSize // restore value of stack pointer
192192
}
193193
}

MyOS_1/Tasks/Context.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "../misc.h"
33
#include "../printf.h"
44
#include <stdint.h>
5+
#include "../paging.h"
56

67
// TODO: Make less ghetto
78
#define MAX_TASKS 64
@@ -58,6 +59,6 @@ extern uint32_t nextPID;
5859

5960
extern bool multiEnable; // TEMPTEMP
6061

61-
void DispatchNewTask(uint32_t programStart, uint32_t newPageDirectory, uint32_t stackSize, const char *imageName, bool exclusive);
62+
void DispatchNewTask(uint32_t programStart, PAGE_DIRECTORY_ENTRY *newPageDirectory, uint32_t stackSize, const char *imageName, bool exclusive);
6263

6364
void SwitchTask(uint32_t taskIndex);

MyOS_1/Tasks/Dispatcher.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "Context.h"
22
#include "../printf.h"
33
#include "../Interrupts/System_Calls.h"
4+
#include "../paging.h"
45

56
PROCESS_CONTROL_BLOCK tasks[64] = { 0 };
67
READY_QUEUE_ENTRY *readyQueueHead = NULL;
@@ -12,7 +13,7 @@ uint32_t nextPID = 1000;
1213
// This must be global so we can access it while playing with the stack in ways the compiler can't predict
1314
uint32_t stackPtr;
1415

15-
void DispatchNewTask(uint32_t programStart, uint32_t newPageDirectory, uint32_t stackSize, const char *imageName, bool exclusive)
16+
void DispatchNewTask(uint32_t programStart, PAGE_DIRECTORY_ENTRY *newPageDirectory, uint32_t stackSize, const char *imageName, bool exclusive)
1617
{
1718
// disable interrupts
1819
__asm cli
@@ -62,7 +63,7 @@ void DispatchNewTask(uint32_t programStart, uint32_t newPageDirectory, uint32_t
6263
strncpy(tasks[taskSlot].imageName, imageName, MAX_IMAGE_NAME_LENGTH - 1);
6364
tasks[taskSlot].inUse = true;
6465
tasks[taskSlot].PID = nextPID++;
65-
tasks[taskSlot].cr3 = newPageDirectory;
66+
tasks[taskSlot].cr3 = (uint32_t)newPageDirectory;
6667

6768
// Create ready queue entry
6869
READY_QUEUE_ENTRY *queueEntry;

MyOS_1/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ void KeStartupPhase2(multiboot_info *multibootInfo)
118118
tasks[0].inUse = true;
119119
strncpy(tasks[0].imageName, "KERNEL PROCESS", sizeof("KERNEL PROCESS"));
120120
tasks[0].PID = nextPID - 1;
121+
tasks[0].cr3 = pageDir;
121122

122123
// Initialize interrupts
123124
Interrupts_Init();

MyOS_1/paging.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
#include "Paging.h"
33
#include "Console_VGA.h"
44
#include <stdint.h>
5-
#include "Interrupts\System_Calls.h"
5+
#include "Interrupts/System_Calls.h"
6+
#include "Tasks/Context.h"
67

78
// Allocate space for paging structures. We need enough space for three arrays with 0x1000 32-bit entries each.
89
// Each array must be aligned on a 0x1000-byte boundary.
@@ -27,6 +28,13 @@ uint32_t nextPageDirectory; // TEMPTEMP where the next created page directory wi
2728
// TODO: Make multi-threading safe
2829
void KPageAllocator(unsigned int pages, unsigned int *pPagesAllocated, uint32_t *pRetVal)
2930
{
31+
// Ensure interrupts are disabled
32+
__asm
33+
{
34+
pushf
35+
cli
36+
}
37+
3038
*pPagesAllocated = 0;
3139

3240
// make sure there's enough pages available
@@ -49,19 +57,42 @@ void KPageAllocator(unsigned int pages, unsigned int *pPagesAllocated, uint32_t
4957

5058
uint32_t nextPage = pagingNextAvailableMemory / FOUR_MEGABYTES;
5159

60+
// Get the page directory we'll be using
61+
PAGE_DIRECTORY_ENTRY *pageDirectory = (PAGE_DIRECTORY_ENTRY *)tasks[currentTask].cr3;
62+
5263
// Add each page to the page directory
5364
for (size_t allocated = 0; allocated < pages; ++allocated)
5465
{
5566
// Add the current page to the page directory
56-
pageDir[nextPage] = ((nextPage * FOUR_MEGABYTES)
67+
pageDirectory[nextPage] = ((nextPage * FOUR_MEGABYTES)
5768
| DIRECTORY_ENTRY_PRESENT | DIRECTORY_ENTRY_USER_ACCESSIBLE | DIRECTORY_ENTRY_WRITABLE | DIRECTORY_ENTRY_4MB);
69+
70+
// If we're mapping this into the kernel space, we need to copy that mapping into every running task
71+
if (pageDirectory == pageDir)
72+
{
73+
for (int i = 0; i < MAX_TASKS; ++i)
74+
{
75+
if (!tasks[i].inUse || tasks[i].cr3 == pageDir)
76+
continue;
77+
78+
PAGE_DIRECTORY_ENTRY *otherPageDir = (PAGE_DIRECTORY_ENTRY *)tasks[i].cr3;
79+
80+
otherPageDir[nextPage] = ((nextPage * FOUR_MEGABYTES)
81+
| DIRECTORY_ENTRY_PRESENT | DIRECTORY_ENTRY_USER_ACCESSIBLE | DIRECTORY_ENTRY_WRITABLE | DIRECTORY_ENTRY_4MB);
82+
}
83+
}
5884

5985
// update pointers and stuff
6086
++nextPage;
6187
--paging4MPagesAvailable;
6288
pagingNextAvailableMemory += FOUR_MEGABYTES;
6389
++(*pPagesAllocated);
6490
}
91+
92+
//kprintf("Task: %s\n", tasks[currentTask].imageName);
93+
//Paging_Print_Page_Table(pageDirectory);
94+
95+
__asm popf
6596
}
6697

6798
bool Paging_Print_Page_Table(PAGE_DIRECTORY_ENTRY *thePageDir)

Release/MyOS_1.dll

512 Bytes
Binary file not shown.

TestApp2/TestApp2.vcxproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,12 @@
130130
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
131131
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
132132
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
133-
<BaseAddress>0xC00000</BaseAddress>
133+
<BaseAddress>0x800000</BaseAddress>
134134
<EntryPointSymbol>main</EntryPointSymbol>
135135
<RandomizedBaseAddress>false</RandomizedBaseAddress>
136136
<StackReserveSize>0x100000</StackReserveSize>
137137
<StackCommitSize>0x100000</StackCommitSize>
138+
<FixedBaseAddress>true</FixedBaseAddress>
138139
</Link>
139140
<PostBuildEvent>
140141
<Command>copy "$(TargetDir)$(TargetFileName)" C:\Users\Administrator\.VirtualBox\TFTP\$(TargetFileName)</Command>

0 commit comments

Comments
 (0)