Skip to content

Commit 972bfb5

Browse files
committed
Fixes ability to load multiple user application at a same time.
1 parent b41dd55 commit 972bfb5

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

memory_layout.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
| 0x7F00 | 0x7FFF | 256B | SHARED STATIC MEMORY for real_mode library |
99
| 0x8000 | 0xB1FF | 12.5KB | BOOT LOADER STAGE 2 |
1010
| 0xC000 | 0x10200 | - | KERNEL |
11-
| 0x20000 | fill | - | Application |
11+
| 0x20000 | 0x2FFFF | - | Application 0 |
12+
| 0x30000 | 0x3FFFF | - | Application 1 |
13+
...
1214
| - | 0xFFFFF | - | 20-bit memory limit |

src/kernel/process.asm

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,33 @@ global call_main
88
push ebp
99
mov ebp, esp
1010

11-
mov eax, [ebp + 0x8] ; (argc)
12-
mov ebx, [ebp + 0xc] ; (argv)
11+
mov eax, [ebp + 0x10] ; (argc)
12+
mov eax, [ebp + 0x14] ; (argv)
13+
14+
; bx and cx are used below.
15+
mov ebx, [ebp + 0x08] ; (CS)
16+
mov ecx, [ebp + 0x0c] ; (DS)
1317

1418
mov eax, esp
1519
mov [kernel_saved_stack_top], eax
1620

21+
1722
; Preparing for exec.
1823

19-
mov ax, 0x30
20-
mov es, ax
21-
mov ss, ax
22-
mov ds, ax
23-
mov fs, ax
24-
mov gs, ax
24+
; Assigning DS and stuff.
25+
mov es, cx
26+
mov ss, cx
27+
mov ds, cx
28+
mov fs, cx
29+
mov gs, cx
2530

2631
mov eax, 0xFFFF
27-
mov esp, eax
28-
call 0x28:0x0000
32+
33+
; far jump to main()
34+
mov [farjmp_location+4], bx
35+
xor ebx, ebx
36+
mov [farjmp_location], ebx
37+
call far [farjmp_location]
2938
; eax should contain the program return value.
3039

3140
; Returned from exec.
@@ -45,4 +54,6 @@ global call_main
4554
ret
4655

4756
[SECTION .data]
48-
kernel_saved_stack_top db ' '
57+
kernel_saved_stack_top db ' '
58+
farjmp_location dd 0
59+
dw 0

src/kernel/process.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ void process_handler_init() {
2424

2525
int process_reserve_new_id() {
2626
// Only one process can at a time for now.
27-
return 0;
2827
// id: application id, 0-based
2928
for (int i = 0; i < MAX_PROCESS; ++i) {
3029
if(process_availability[i]) {
@@ -40,10 +39,10 @@ int process_free_id(int id) {
4039
}
4140

4241
int process_new_allocated_memory(int id) {
43-
return MEMORY_LOCATION_APP+0x10000;
42+
return MEMORY_LOCATION_APP+0x10000*id;
4443
}
4544

46-
extern int call_main(int argc, char *argv[]);
45+
extern int call_main(int cs, int ds, int argc, char *argv[]);
4746

4847
int process_exec(int sector_index, int sector_count) {
4948
int id = process_reserve_new_id();
@@ -78,9 +77,17 @@ int process_exec(int sector_index, int sector_count) {
7877

7978
int relative_address = memory_location-MEMORY_LOCATION_KERNEL;
8079
print_log("App loaded at 0x%x, relative_address: 0x%x: %x...",
81-
MEMORY_LOCATION_APP, relative_address,
80+
memory_location, relative_address,
8281
*(int*)relative_address);
83-
int exit_code = call_main(0, 0);
82+
83+
int code_segment = idt_cs_entry*sizeof(struct GDTEntry);
84+
int data_segment = idt_ds_entry*sizeof(struct GDTEntry);
85+
int argc = 0;
86+
int argv = 0;
87+
print_log("call_main(0x%x, 0x%x, %d, %d)", code_segment, data_segment, argc, argv);
88+
89+
int exit_code = call_main(code_segment, data_segment, argc, argv);
90+
8491
process_free_id(id);
8592
io_low_flush();
8693
return exit_code;

0 commit comments

Comments
 (0)