Skip to content

Commit 99cf771

Browse files
author
tthomps
committed
Store a reference to a page directory (cr3 register) with each task and switch the cr3 register with each context switch.
1 parent a963d12 commit 99cf771

File tree

4 files changed

+10
-4
lines changed

4 files changed

+10
-4
lines changed

MyOS_1/Tasks/Context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ typedef struct CONTEXT_INFO
2222
bool inUse;
2323
bool exclusive; // True to disable switching to any other tasks
2424
uint32_t PID;
25+
uint32_t cr3;
2526
// ? What else?
2627
} CONTEXT_INFO, PROCESS_CONTROL_BLOCK;
2728

MyOS_1/Tasks/Dispatcher.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void DispatchNewTask(uint32_t programStart, uint32_t stackSize, const char *imag
9999
push [stackPtr]
100100
int SYSCALL_DISPATCH_NEW_TASK // call dispatch_new_task_interrupt_handler(eflags, cs, stackPtr, esp)
101101
add esp, 8
102-
}
102+
}
103103

104104
// Update the task's "stack image" to represent values appropriate for the new task
105105
TASK_STACK_LAYOUT *pNewTask = (TASK_STACK_LAYOUT *)((unsigned long)stackPtr - sizeof(TASK_STACK_LAYOUT));
@@ -110,6 +110,7 @@ void DispatchNewTask(uint32_t programStart, uint32_t stackSize, const char *imag
110110

111111
tasks[taskSlot].ESP = (uint32_t)pNewTask;
112112
tasks[taskSlot].exclusive = exclusive;
113+
tasks[taskSlot].cr3 = __readcr3(); // TEMPTEMP: Give the new task the same page table as the kernel
113114

114115
// If a GUI shell is running, tell it about the new process
115116
if (guiCallback)

MyOS_1/Timers/PIT.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ void _declspec(naked) timer_interrupt_handler(void)
8585
if (ticksLeftInTask <= 0 && multiEnable)
8686
{
8787
if(debugLevel)
88-
kprintf("Saving %s\n", tasks[currentTask].imageName);
88+
kprintf("Saving %s.", tasks[currentTask].imageName);
8989

9090
// Save current context
9191
_asm mov[espVal], esp
9292
tasks[currentTask].ESP = espVal;
93+
tasks[currentTask].cr3 = __readcr3();
9394

9495
oldTaskIndex = currentTask;
9596

@@ -114,13 +115,16 @@ void _declspec(naked) timer_interrupt_handler(void)
114115
else
115116
{
116117
readyQueueHead->taskIndex = oldTaskIndex;
117-
}
118+
}
118119

119120
if(debugLevel)
120-
kprintf("Switching to %s\n", tasks[currentTask].imageName);
121+
kprintf(" Switching to %s\n", tasks[currentTask].imageName);
121122

122123
ticksLeftInTask = TICKS_PER_TASK;
123124

125+
// switch to the new task's page tables
126+
__writecr3(tasks[currentTask].cr3);
127+
124128
// Get the stack pointer of the next waiting task and make that the stack
125129
espVal = tasks[currentTask].ESP;
126130

Release/MyOS_1.dll

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)