Skip to content

Commit 3ec9810

Browse files
committed
Lecture 114 - Making the process list use vectors
1 parent 27bdced commit 3ec9810

3 files changed

Lines changed: 94 additions & 47 deletions

File tree

PeachOS64Bit/src/kernel.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ void kernel_main()
178178
// load the tss
179179
tss_load(KERNEL_LONG_MODE_TSS_SELECTOR);
180180

181+
// Initialize the process system
182+
process_system_init();
183+
181184
print("tss load was fine\n");
182185
// Register isr80h commands
183186
isr80h_register_commands();

PeachOS64Bit/src/task/process.c

Lines changed: 90 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,23 @@
1515
// The current process that is running
1616
struct process *current_process = 0;
1717

18-
static struct process *processes[PEACHOS_MAX_PROCESSES] = {};
18+
struct vector *process_vector = NULL;
1919

20-
int process_get_allocation_by_start_addr(struct process *process, void *addr, struct process_allocation* allocation_out);
20+
int process_get_allocation_by_start_addr(struct process *process, void *addr, struct process_allocation *allocation_out);
2121

2222
int process_free_process(struct process *process);
2323
int process_close_file_handles(struct process *process);
2424

25-
26-
void* process_virtual_address_to_physical(struct process* process, void* virt_addr)
25+
void *process_virtual_address_to_physical(struct process *process, void *virt_addr)
2726
{
2827
return paging_get_physical_address(process->paging_desc, virt_addr);
2928
}
3029

30+
void process_system_init()
31+
{
32+
process_vector = vector_new(sizeof(struct process *), 10, 0);
33+
}
34+
3135
static void process_init(struct process *process)
3236
{
3337
memset(process, 0, sizeof(struct process));
@@ -42,12 +46,15 @@ struct process *process_current()
4246

4347
struct process *process_get(int process_id)
4448
{
45-
if (process_id < 0 || process_id >= PEACHOS_MAX_PROCESSES)
49+
int res = 0;
50+
struct process *process_out = NULL;
51+
res = vector_at(process_vector, process_id, &process_out, sizeof(process_out));
52+
if (res < 0)
4653
{
47-
return NULL;
54+
return ERROR(EINVARG);
4855
}
4956

50-
return processes[process_id];
57+
return process_out;
5158
}
5259

5360
int process_switch(struct process *process)
@@ -185,18 +192,18 @@ static void process_allocation_unjoin(struct process *process, void *ptr)
185192
}
186193
}
187194

188-
int process_get_allocation_by_start_addr(struct process *process, void *addr, struct process_allocation* allocation_out)
195+
int process_get_allocation_by_start_addr(struct process *process, void *addr, struct process_allocation *allocation_out)
189196
{
190197
size_t total_allocations = vector_count(process->allocations);
191-
for(size_t i = 0; i < total_allocations; i++)
198+
for (size_t i = 0; i < total_allocations; i++)
192199
{
193200
struct process_allocation allocation;
194201
int res = vector_at(process->allocations, i, &allocation, sizeof(allocation));
195202
if (res < 0)
196203
{
197204
break;
198205
}
199-
if(allocation.ptr == addr)
206+
if (allocation.ptr == addr)
200207
{
201208
*allocation_out = allocation;
202209
return 0;
@@ -265,22 +272,29 @@ int process_free_program_data(struct process *process)
265272

266273
void process_switch_to_any()
267274
{
268-
for (int i = 0; i < PEACHOS_MAX_PROCESSES; i++)
275+
size_t total_process_slots = vector_count(process_vector);
276+
for(size_t i = 0; i < total_process_slots; i++)
269277
{
270-
if (processes[i])
278+
struct process* process = NULL;
279+
int res = vector_at(process_vector, i, &process, sizeof(&process));
280+
if (res < 0)
281+
{
282+
break;
283+
}
284+
285+
if (process)
271286
{
272-
process_switch(processes[i]);
287+
process_switch(process);
273288
return;
274289
}
275290
}
276-
277291
panic("No processes to switch too\n");
278292
}
279293

280294
static void process_unlink(struct process *process)
281295
{
282-
processes[process->id] = 0x00;
283-
296+
struct process* null_process = NULL;
297+
vector_overwrite(process_vector, process->id, &null_process, sizeof(&null_process));
284298
if (current_process == process)
285299
{
286300
process_switch_to_any();
@@ -524,7 +538,7 @@ int process_map_memory(struct process *process)
524538
{
525539
int res = 0;
526540

527-
// Map all the e820 memory regions
541+
// Map all the e820 memory regions
528542
// so the whole address space is mapped
529543
paging_map_e820_memory_regions(process->paging_desc);
530544

@@ -555,13 +569,41 @@ int process_map_memory(struct process *process)
555569

556570
int process_get_free_slot()
557571
{
558-
for (int i = 0; i < PEACHOS_MAX_PROCESSES; i++)
572+
int res = 0;
573+
bool found = false;
574+
size_t total_process_slots = vector_count(process_vector);
575+
for(size_t i = 0; i < total_process_slots; i++)
559576
{
560-
if (processes[i] == 0)
561-
return i;
577+
struct process* process_out = NULL;
578+
res = vector_at(process_vector, i, &process_out, sizeof(process_out));
579+
if (res < 0)
580+
{
581+
break;
582+
}
583+
584+
if (!process_out)
585+
{
586+
found = true;
587+
res = i;
588+
break;
589+
}
562590
}
563591

564-
return -EISTKN;
592+
if (res < 0)
593+
{
594+
goto out;
595+
}
596+
597+
if (!found)
598+
{
599+
struct process* null_process = NULL;
600+
int process_index = vector_push(process_vector, &null_process);
601+
602+
// vector_push returned the index of the new null process pointer
603+
res = process_index;
604+
}
605+
out:
606+
return res;
565607
}
566608

567609
int process_load(const char *filename, struct process **process)
@@ -636,7 +678,7 @@ int process_load_for_slot(const char *filename, struct process **process, int pr
636678
if (res < 0)
637679
{
638680
goto out;
639-
}
681+
}
640682

641683
// Create a task
642684
_process->task = task_new(_process);
@@ -651,8 +693,9 @@ int process_load_for_slot(const char *filename, struct process **process, int pr
651693

652694
*process = _process;
653695

654-
// Add the process to the array
655-
processes[process_slot] = _process;
696+
// Overwrite the free process pointer thats in the vector
697+
// with our allocated one. SO we take ownership of the slot.
698+
vector_overwrite(process_vector, process_slot, &_process, sizeof(&_process));
656699

657700
out:
658701
if (ISERR(res))
@@ -669,13 +712,13 @@ int process_load_for_slot(const char *filename, struct process **process, int pr
669712
return res;
670713
}
671714

672-
bool process_is_stack_memory(struct process* process, void* addr)
715+
bool process_is_stack_memory(struct process *process, void *addr)
673716
{
674-
return (uintptr_t) addr >= PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_END &&
675-
(uintptr_t) addr <= PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START;
717+
return (uintptr_t)addr >= PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_END &&
718+
(uintptr_t)addr <= PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START;
676719
}
677720

678-
int process_get_allocation_by_addr(struct process* process, void* addr, struct process_allocation_request* allocation_request_out)
721+
int process_get_allocation_by_addr(struct process *process, void *addr, struct process_allocation_request *allocation_request_out)
679722
{
680723
// Null the request
681724
memset(allocation_request_out, 0, sizeof(struct process_allocation_request));
@@ -684,23 +727,23 @@ int process_get_allocation_by_addr(struct process* process, void* addr, struct p
684727
if (process_is_stack_memory(process, addr))
685728
{
686729
// we have stack memory
687-
uint64_t addr_int = (uint64_t) addr;
730+
uint64_t addr_int = (uint64_t)addr;
688731
uint64_t stack_size = PEACHOS_USER_PROGRAM_STACK_SIZE;
689732
// START OF THE STACK IS HIGHER IN MEMORY REMEMBER
690733
uint64_t total_bytes_left = PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START - addr_int;
691-
allocation_request_out->allocation.ptr = (void*) PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_END;
692-
allocation_request_out->allocation.end = (void*) PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START;
734+
allocation_request_out->allocation.ptr = (void *)PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_END;
735+
allocation_request_out->allocation.end = (void *)PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START;
693736
allocation_request_out->allocation.size = stack_size;
694737
allocation_request_out->flags |= PROCESS_ALLOCATION_REQUEST_IS_STACK_MEMORY;
695738
allocation_request_out->peek.addr = addr;
696-
allocation_request_out->peek.end = (void*) PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START;
739+
allocation_request_out->peek.end = (void *)PEACHOS_PROGRAM_VIRTUAL_STACK_ADDRESS_START;
697740
allocation_request_out->peek.total_bytes_left = total_bytes_left;
698741
return 0;
699742
}
700743

701744
// Not a stack address then check the heap
702745
size_t total_allocations = vector_count(process->allocations);
703-
for(size_t i = 0; i < total_allocations; i++)
746+
for (size_t i = 0; i < total_allocations; i++)
704747
{
705748
struct process_allocation allocation;
706749
int res = vector_at(process->allocations, i, &allocation, sizeof(allocation));
@@ -709,16 +752,16 @@ int process_get_allocation_by_addr(struct process* process, void* addr, struct p
709752
break;
710753
}
711754

712-
uint64_t allocation_addr = (uint64_t) allocation.ptr;
713-
uint64_t allocation_addr_end = (uint64_t) allocation.end;
714-
if ((uint64_t) addr >= allocation_addr &&
715-
((uint64_t) addr) <= allocation_addr_end)
755+
uint64_t allocation_addr = (uint64_t)allocation.ptr;
756+
uint64_t allocation_addr_end = (uint64_t)allocation.end;
757+
if ((uint64_t)addr >= allocation_addr &&
758+
((uint64_t)addr) <= allocation_addr_end)
716759
{
717-
size_t bytes_used = (uint64_t) addr - allocation_addr;
760+
size_t bytes_used = (uint64_t)addr - allocation_addr;
718761
size_t bytes_left = allocation_addr_end - bytes_used;
719762
allocation_request_out->allocation = allocation;
720763
allocation_request_out->peek.addr = addr;
721-
allocation_request_out->peek.end = (void*) allocation_addr_end;
764+
allocation_request_out->peek.end = (void *)allocation_addr_end;
722765
allocation_request_out->peek.total_bytes_left = bytes_left;
723766
return 0;
724767
}
@@ -727,7 +770,7 @@ int process_get_allocation_by_addr(struct process* process, void* addr, struct p
727770
return -EIO;
728771
}
729772

730-
int process_validate_memory_or_terminate(struct process* process, void* virt_addr, size_t space_needed)
773+
int process_validate_memory_or_terminate(struct process *process, void *virt_addr, size_t space_needed)
731774
{
732775
int res = 0;
733776
struct process_allocation_request allocation_request;
@@ -739,7 +782,7 @@ int process_validate_memory_or_terminate(struct process* process, void* virt_add
739782

740783
if (allocation_request.peek.total_bytes_left < space_needed)
741784
{
742-
res =-EINVARG;
785+
res = -EINVARG;
743786
goto out;
744787
}
745788
out:
@@ -749,16 +792,16 @@ int process_validate_memory_or_terminate(struct process* process, void* virt_add
749792
}
750793
return res;
751794
}
752-
int process_fstat(struct process* process, int fd, struct file_stat* virt_filestat_addr)
795+
int process_fstat(struct process *process, int fd, struct file_stat *virt_filestat_addr)
753796
{
754797
int res = 0;
755798
res = process_validate_memory_or_terminate(process, virt_filestat_addr, sizeof(*virt_filestat_addr));
756799
if (res < 0)
757800
{
758801
goto out;
759-
}
802+
}
760803

761-
struct file_stat* phys_filestat_addr = process_virtual_address_to_physical(process, virt_filestat_addr);
804+
struct file_stat *phys_filestat_addr = process_virtual_address_to_physical(process, virt_filestat_addr);
762805
if (!phys_filestat_addr)
763806
{
764807
res = -EINVARG;
@@ -773,11 +816,11 @@ int process_fstat(struct process* process, int fd, struct file_stat* virt_filest
773816

774817
out:
775818
return res;
776-
}
777-
int process_fseek(struct process* process, int fd, int offset, FILE_SEEK_MODE whence)
819+
}
820+
int process_fseek(struct process *process, int fd, int offset, FILE_SEEK_MODE whence)
778821
{
779822
int res = 0;
780-
struct process_file_handle* handle = process_file_handle_get(process, fd);
823+
struct process_file_handle *handle = process_file_handle_get(process, fd);
781824
if (!handle)
782825
{
783826
res = -EIO;

PeachOS64Bit/src/task/process.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ struct process
114114
struct process_arguments arguments;
115115
};
116116

117+
void process_system_init();
117118
int process_switch(struct process* process);
118119
int process_load_switch(const char* filename, struct process** process);
119120
int process_load(const char* filename, struct process** process);

0 commit comments

Comments
 (0)