Skip to content

Commit 27bdced

Browse files
committed
Lecture 113 - Moving the task paging descriptor into the process
1 parent e6a9dc3 commit 27bdced

4 files changed

Lines changed: 34 additions & 30 deletions

File tree

PeachOS64Bit/src/task/process.c

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ int process_close_file_handles(struct process *process);
2525

2626
void* process_virtual_address_to_physical(struct process* process, void* virt_addr)
2727
{
28-
return paging_get_physical_address(process->task->paging_desc, virt_addr);
28+
return paging_get_physical_address(process->paging_desc, virt_addr);
2929
}
3030

3131
static void process_init(struct process *process)
@@ -88,7 +88,7 @@ int process_find_free_allocation_index(struct process *process)
8888

8989
int process_allocation_set_map(struct process *process, int allocation_entry_index, void *ptr, size_t size)
9090
{
91-
int res = paging_map_to(process->task->paging_desc, ptr, ptr, paging_align_address(ptr + size), PAGING_IS_WRITEABLE | PAGING_IS_PRESENT | PAGING_ACCESS_FROM_ALL);
91+
int res = paging_map_to(process->paging_desc, ptr, ptr, paging_align_address(ptr + size), PAGING_IS_WRITEABLE | PAGING_IS_PRESENT | PAGING_ACCESS_FROM_ALL);
9292
if (res < 0)
9393
{
9494
goto out;
@@ -402,7 +402,7 @@ void process_free(struct process *process, void *ptr)
402402
return;
403403
}
404404

405-
res = paging_map_to(process->task->paging_desc, allocation.ptr, allocation.ptr, paging_align_address(allocation.ptr + allocation.size), 0x00);
405+
res = paging_map_to(process->paging_desc, allocation.ptr, allocation.ptr, paging_align_address(allocation.ptr + allocation.size), 0x00);
406406
if (res < 0)
407407
{
408408
return;
@@ -492,7 +492,7 @@ static int process_load_data(const char *filename, struct process *process)
492492
int process_map_binary(struct process *process)
493493
{
494494
int res = 0;
495-
paging_map_to(process->task->paging_desc, (void *)PEACHOS_PROGRAM_VIRTUAL_ADDRESS, process->ptr, paging_align_address(process->ptr + process->size), PAGING_IS_PRESENT | PAGING_ACCESS_FROM_ALL | PAGING_IS_WRITEABLE);
495+
paging_map_to(process->paging_desc, (void *)PEACHOS_PROGRAM_VIRTUAL_ADDRESS, process->ptr, paging_align_address(process->ptr + process->size), PAGING_IS_PRESENT | PAGING_ACCESS_FROM_ALL | PAGING_IS_WRITEABLE);
496496
return res;
497497
}
498498

@@ -512,7 +512,7 @@ static int process_map_elf(struct process *process)
512512
{
513513
flags |= PAGING_IS_WRITEABLE;
514514
}
515-
res = paging_map_to(process->task->paging_desc, paging_align_to_lower_page((void *)(uintptr_t)phdr->p_vaddr), paging_align_to_lower_page(phdr_phys_address), paging_align_address(phdr_phys_address + phdr->p_memsz), flags);
515+
res = paging_map_to(process->paging_desc, paging_align_to_lower_page((void *)(uintptr_t)phdr->p_vaddr), paging_align_to_lower_page(phdr_phys_address), paging_align_address(phdr_phys_address + phdr->p_memsz), flags);
516516
if (ISERR(res))
517517
{
518518
break;
@@ -524,6 +524,10 @@ int process_map_memory(struct process *process)
524524
{
525525
int res = 0;
526526

527+
// Map all the e820 memory regions
528+
// so the whole address space is mapped
529+
paging_map_e820_memory_regions(process->paging_desc);
530+
527531
switch (process->filetype)
528532
{
529533
case PROCESS_FILETYPE_ELF:
@@ -544,7 +548,7 @@ int process_map_memory(struct process *process)
544548
}
545549

546550
// Finally map the stack
547-
paging_map_to(process->task->paging_desc, (void *)PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_END, process->stack, paging_align_address(process->stack + PEACHOS_USER_PROGRAM_STACK_SIZE), PAGING_IS_PRESENT | PAGING_ACCESS_FROM_ALL | PAGING_IS_WRITEABLE);
551+
paging_map_to(process->paging_desc, (void *)PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_END, process->stack, paging_align_address(process->stack + PEACHOS_USER_PROGRAM_STACK_SIZE), PAGING_IS_PRESENT | PAGING_ACCESS_FROM_ALL | PAGING_IS_WRITEABLE);
548552
out:
549553
return res;
550554
}
@@ -621,6 +625,19 @@ int process_load_for_slot(const char *filename, struct process **process, int pr
621625
strncpy(_process->filename, filename, sizeof(_process->filename));
622626
_process->id = process_slot;
623627

628+
_process->paging_desc = paging_desc_new(PAGING_MAP_LEVEL_4);
629+
if (!_process->paging_desc)
630+
{
631+
res = -EIO;
632+
goto out;
633+
}
634+
635+
res = process_map_memory(_process);
636+
if (res < 0)
637+
{
638+
goto out;
639+
}
640+
624641
// Create a task
625642
_process->task = task_new(_process);
626643
if (ERROR_I(_process->task) == 0)
@@ -632,12 +649,6 @@ int process_load_for_slot(const char *filename, struct process **process, int pr
632649
goto out;
633650
}
634651

635-
res = process_map_memory(_process);
636-
if (res < 0)
637-
{
638-
goto out;
639-
}
640-
641652
*process = _process;
642653

643654
// Add the process to the array
@@ -759,7 +770,7 @@ int process_fstat(struct process* process, int fd, struct file_stat* virt_filest
759770
{
760771
goto out;
761772
}
762-
773+
763774
out:
764775
return res;
765776
}

PeachOS64Bit/src/task/process.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ struct process
7272
// The main process task
7373
struct task* task;
7474

75+
/**
76+
* The page directory of the process virtual memory.
77+
*/
78+
struct paging_desc* paging_desc;
79+
80+
81+
7582
// The memory (malloc) allocations of the process
7683
struct vector* allocations;
7784

PeachOS64Bit/src/task/task.c

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ static void task_list_remove(struct task *task)
9797

9898
int task_free(struct task *task)
9999
{
100-
paging_desc_free(task->paging_desc);
101100
task_list_remove(task);
102101

103102
// Finally free the task data
@@ -120,13 +119,13 @@ void task_next()
120119
int task_switch(struct task *task)
121120
{
122121
current_task = task;
123-
paging_switch(task->paging_desc);
122+
paging_switch(task->process->paging_desc);
124123
return 0;
125124
}
126125

127126
struct paging_desc* task_paging_desc(struct task* task)
128127
{
129-
return task->paging_desc;
128+
return task->process->paging_desc;
130129
}
131130

132131
struct paging_desc* task_current_paging_desc()
@@ -240,14 +239,6 @@ void task_run_first_ever_task()
240239
int task_init(struct task *task, struct process *process)
241240
{
242241
memset(task, 0, sizeof(struct task));
243-
// Map the entire 4GB address space to its self
244-
task->paging_desc = paging_desc_new(PAGING_MAP_LEVEL_4);
245-
if (!task->paging_desc)
246-
{
247-
return -EIO;
248-
}
249-
250-
paging_map_e820_memory_regions(task->paging_desc);
251242

252243
task->registers.ip = PEACHOS_PROGRAM_VIRTUAL_ADDRESS;
253244
if (process->filetype == PROCESS_FILETYPE_ELF)
@@ -282,5 +273,5 @@ void* task_get_stack_item(struct task* task, int index)
282273

283274
void* task_virtual_address_to_physical(struct task* task, void* virtual_address)
284275
{
285-
return paging_get_physical_address(task->paging_desc, virtual_address);
276+
return paging_get_physical_address(task->process->paging_desc, virtual_address);
286277
}

PeachOS64Bit/src/task/task.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,6 @@ struct registers
2626
struct process;
2727
struct task
2828
{
29-
/**
30-
* The page directory of the task.
31-
*/
32-
struct paging_desc* paging_desc;
33-
3429
// The registers of the task when the task is not running
3530
struct registers registers;
3631

0 commit comments

Comments
 (0)