Skip to content

Commit 6113b2e

Browse files
author
tthomps
committed
Allow executables to be mapped to a virtual memory address. Now (finally) multiple executables can share the same logical address, and the GUI shell supports displaying the output of multiple programs.
1 parent c2da413 commit 6113b2e

File tree

9 files changed

+36
-9
lines changed

9 files changed

+36
-9
lines changed

Media/Screenshots/MyOS_GUI_1.png

106 KB
Loading

MyOS_1/Executables/PE32.c

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "../printf.h"
77
#include "../Tasks/Context.h"
88
#include "../paging.h"
9+
#include "../../MyOS_GUI_Shell/GUI_Kernel_Shell.h"
910

1011
jmp_buf peReturnBuf;
1112

@@ -86,23 +87,40 @@ bool loadAndRunPE(uint8_t *executableDestination, DOS_Header *mzAddress, const c
8687

8788
// Get a new page directory for the new task
8889
PAGE_DIRECTORY_ENTRY *newPageDirectory;
89-
90-
// GUI.exe shares the kernel's page directory
91-
if ((uint32_t)executableDestination != 0xC00000)
90+
uint32_t physicalLocation = (uint32_t)executableDestination;
91+
92+
// Create a new page directory unless we're loading the GUI, because GUI.exe shares the kernel's page directory and uses a hardcoded address
93+
if ((uint32_t)executableDestination != GUI_BASE_ADDRESS)
9294
{
9395
newPageDirectory = (PAGE_DIRECTORY_ENTRY *)nextPageDirectory;
9496
nextPageDirectory += sizeof(PAGE_DIRECTORY_ENTRY) * 1024;
9597

9698
// Copy the kernel's page table to this new page table
9799
memcpy((void*)newPageDirectory, pageDir, sizeof(PAGE_DIRECTORY_ENTRY) * 1024);
98100

101+
// Allocate a page of memory for the new application
102+
unsigned int pagesAllocated;
103+
KPageAllocator(1, &pagesAllocated, &physicalLocation);
104+
if (!pagesAllocated)
105+
{
106+
kprintf("Unable to allocate memory for %s", imageName);
107+
_enable();
108+
return false;
109+
}
110+
111+
//kprintf("Got a page of memory at 0x%X\n", physicalLocation);
112+
113+
// Map the page of memory into the process' page directory
114+
newPageDirectory[(uint32_t)executableDestination / FOUR_MEGABYTES] = physicalLocation
115+
| DIRECTORY_ENTRY_PRESENT | DIRECTORY_ENTRY_WRITABLE | DIRECTORY_ENTRY_4MB | DIRECTORY_ENTRY_USER_ACCESSIBLE;
116+
99117
//Paging_Print_Page_Table(newPageDirectory);
100118
}
101119
else
102120
newPageDirectory = pageDir;
103121

104122
// TEMPTEMP - zero out 5 0x1000 sections of memory (tailored to TestApp1.exe)
105-
memset(executableDestination, 0, 0x5000);
123+
memset(physicalLocation, 0, 0x5000);
106124

107125
// Get the address of the first section
108126
IMAGE_SECTION_HEADER *sectionHeader = (IMAGE_SECTION_HEADER*)((uint32_t)directory + sizeof(IMAGE_DATA_DIRECTORY));
@@ -120,7 +138,7 @@ bool loadAndRunPE(uint8_t *executableDestination, DOS_Header *mzAddress, const c
120138
}
121139

122140
// Determine the destination of the current section
123-
uint8_t *sectionDest = (uint8_t*)((uint32_t)executableDestination + sectionHeader->mVirtualAddress);
141+
uint8_t *sectionDest = (uint8_t*)((uint32_t)physicalLocation + sectionHeader->mVirtualAddress);
124142
if (debugLevel)
125143
{
126144
terminal_writestring("Virtual address of section: ");

MyOS_1/Timers/PIT.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ void _declspec(naked) timer_interrupt_handler(void)
123123
ticksLeftInTask = TICKS_PER_TASK;
124124

125125
// switch to the new task's page tables
126-
__writecr3(tasks[currentTask].cr3);
126+
if(tasks[currentTask].cr3 != __readcr3())
127+
__writecr3(tasks[currentTask].cr3);
127128

128129
// Get the stack pointer of the next waiting task and make that the stack
129130
espVal = tasks[currentTask].ESP;

MyOS_1/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +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;
121+
tasks[0].cr3 = (uint32_t)pageDir;
122122

123123
// Initialize interrupts
124124
Interrupts_Init();

MyOS_1/paging.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void KPageAllocator(unsigned int pages, unsigned int *pPagesAllocated, uint32_t
7272
{
7373
for (int i = 0; i < MAX_TASKS; ++i)
7474
{
75-
if (!tasks[i].inUse || tasks[i].cr3 == pageDir)
75+
if (!tasks[i].inUse || tasks[i].cr3 == (uint32_t)pageDir)
7676
continue;
7777

7878
PAGE_DIRECTORY_ENTRY *otherPageDir = (PAGE_DIRECTORY_ENTRY *)tasks[i].cr3;

MyOS_GUI_Shell/GUI_Kernel_Shell.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ extern "C" {
88

99
// Used to define the interface between the Kernel and the GUI Shell (on the shell side)
1010

11+
#define \
12+
GUI_BASE_ADDRESS 0xC00000
13+
1114
// Called by the kernel to send messages to the GUI
1215
void GUI_Kernel_Callback(uint32_t PID, uint32_t messageType, void *pData);
1316

MyOS_GUI_Shell/MyOS_GUI_Shell.vcxproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,14 +133,15 @@
133133
<AdditionalDependencies>%(AdditionalDependencies)</AdditionalDependencies>
134134
<IgnoreAllDefaultLibraries>true</IgnoreAllDefaultLibraries>
135135
<LinkTimeCodeGeneration>Default</LinkTimeCodeGeneration>
136-
<BaseAddress>0xC00000</BaseAddress>
136+
<BaseAddress>@GUI_Kernel_Shell.h,GUI_BASE_ADDRESS</BaseAddress>
137137
<DataExecutionPrevention>false</DataExecutionPrevention>
138138
<MapFileName>GUI.map</MapFileName>
139139
<MapExports>true</MapExports>
140140
<RandomizedBaseAddress>false</RandomizedBaseAddress>
141141
<EntryPointSymbol>main</EntryPointSymbol>
142142
<GenerateMapFile>true</GenerateMapFile>
143143
<ImageHasSafeExceptionHandlers>false</ImageHasSafeExceptionHandlers>
144+
<FixedBaseAddress>true</FixedBaseAddress>
144145
</Link>
145146
<PostBuildEvent>
146147
<Message>Copying GUI.exe to TFTP directory</Message>
@@ -294,6 +295,7 @@
294295
<ClInclude Include="GUI_Taskbar.h" />
295296
<ClInclude Include="GUI_TerminalWindow.h" />
296297
<ClInclude Include="GUI_Window.h" />
298+
<ClInclude Include="MyOS_GUI_Shell.h" />
297299
</ItemGroup>
298300
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
299301
<ImportGroup Label="ExtensionTargets">

MyOS_GUI_Shell/MyOS_GUI_Shell.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,5 +386,8 @@
386386
<ClInclude Include="GUI_TerminalWindow.h">
387387
<Filter>Header Files</Filter>
388388
</ClInclude>
389+
<ClInclude Include="MyOS_GUI_Shell.h">
390+
<Filter>Header Files</Filter>
391+
</ClInclude>
389392
</ItemGroup>
390393
</Project>

Release/MyOS_1.dll

512 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)