Skip to content

Commit da6ef16

Browse files
committed
Move user and kernel stack init as constant
1 parent 3afda1a commit da6ef16

File tree

7 files changed

+30
-82
lines changed

7 files changed

+30
-82
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ endif
7575
# Tools
7676
HOST_CC = gcc -std=c11 -Iinclude
7777

78-
NASM=nasm -f elf32 $(NASM_DEBUG)
78+
NASM=nasm -f elf32 -i include/ $(NASM_DEBUG)
7979

8080
CC=gcc -std=c11 -fno-builtin -Os -nostartfiles -nostdlib -static $(CC_DEBUG)
8181
KERNEL_CC = $(CC) -m32 -fno-pie -Isrc --sysroot=$(BUILD_DIR) -Iinclude -Isrc/usr/include

include/fuzzy/kernel/interrupts/timer.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ void interrupt_pit_enable();
44

55
void interrupt_register_0x20_irq0_pit();
66

7-
void create_infant_process_irq0_stack(int ds_ss_es_fs);
7+
int create_infant_process_irq0_stack(int ds_ss_es_fs);

include/fuzzy/memmgr/layout.asm

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
; constants only
2+
3+
; Kernel core is app with pid 0
4+
; Kernel event is trigged via IRQ0 or syscall,
5+
6+
STACKINIT_APP EQU 0xFFF0
7+
STACKINIT_KERNEL_CORE EQU STACKINIT_APP
8+
STACKINIT_KERNEL_EVENT EQU 0xC000
9+

src/kernel/core.asm

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
%include "fuzzy/memmgr/layout.asm"
2+
13
[BITS 32]
24

35
extern kernel_core_entry
@@ -14,9 +16,7 @@ global kernel_core_entry_asm
1416
mov fs, ax
1517
mov gs, ax
1618

17-
; TODO: Increase kernel code stack memory size
18-
; The value also depends on kernel handler stack size
19-
mov esp, 0xC000 ; init stack pointer
19+
mov esp, STACKINIT_KERNEL_CORE ; init stack pointer
2020
jmp kernel_core_entry
2121

2222
; kernel_core_entry_asm currently exists only for tests.

src/kernel/interrupts/timer.asm

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
%include "fuzzy/memmgr/layout.asm"
2+
13
[BITS 32]
24

35
global irq0_pit_handler_low
@@ -25,7 +27,7 @@ global create_infant_process_irq0_stack
2527
mov esi, esp
2628

2729
; new stack and segment area
28-
mov esp, 0xFFF0 ; keep offset in sync with process_create(...) and create_infant_process_irq0_stack
30+
mov esp, STACKINIT_KERNEL_EVENT
2931
mov eax, 0x10
3032
mov ss, eax
3133
mov ds, eax
@@ -82,6 +84,8 @@ global create_infant_process_irq0_stack
8284
iret
8385

8486
create_infant_process_irq0_stack:
87+
; keep in sync with _int_irq0_start
88+
; return the esp for user stack
8589
push ebp
8690
mov ebp, esp
8791

@@ -95,9 +99,8 @@ global create_infant_process_irq0_stack
9599

96100
mov ds, ecx
97101

98-
; user stack creation start
99102
; user initial stack
100-
mov eax, 0xFFF0 ; keep in sync with _int_irq0_start
103+
mov eax, STACKINIT_APP
101104
; kernel offset
102105
xor ecx, ecx
103106
mov [eax-0], ecx ; user: eflag
@@ -116,9 +119,7 @@ global create_infant_process_irq0_stack
116119
mov [eax-52], ecx ; user: fs
117120
mov [eax-56], ecx ; user: gs
118121

119-
; user: esp = eax-56 = 0xFFF0-56
120-
; should be compatible with process_create
121-
; user stack creation end
122+
sub eax, 56 ; user stack pointer
122123

123124
pop ds
124125

src/kernel/process/allocation.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,15 +106,6 @@ int process_create() {
106106
int idt_cs_entry = get_idt_cs_entry(pid);
107107
int idt_ds_entry = get_idt_ds_entry(pid);
108108

109-
process->state = STATE_LOADING;
110-
process->cs = get_gdt_number_from_entry_id(idt_cs_entry);
111-
process->ip = 0; //
112-
// initially ds == ss
113-
process->ss = get_gdt_number_from_entry_id(idt_ds_entry);
114-
// should be compatible with create_infant_process_irq0_stack
115-
process->sp = 0xFFF0-56; // keep offset in sync with _int_irq0_start
116-
117-
118109
// Potential improvement:
119110
// - avoid populating GDT entry if already exists.
120111

@@ -130,7 +121,15 @@ int process_create() {
130121
memory_location, memory_location+memory_size-1,
131122
0b0100, // 32-bit protected mode
132123
0x92);
133-
create_infant_process_irq0_stack(process->ss);
124+
125+
126+
// update process state
127+
process->state = STATE_LOADING;
128+
process->cs = get_gdt_number_from_entry_id(idt_cs_entry);
129+
process->ip = 0;
130+
// initially ds == ss
131+
process->ss = get_gdt_number_from_entry_id(idt_ds_entry);
132+
process->sp = create_infant_process_irq0_stack(process->ss);
134133
return pid;
135134
}
136135

src/kernel/process/process.asm

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,10 @@
11
[BITS 32]
22

3-
global call_main
43
global syscall_strncpy_user_to_kernel
54
global syscall_strncpy_kernel_to_user
65

76
[SECTION .text]
87

9-
call_main:
10-
; TODO: write assumptions and constraints
11-
; TODO: function needs to be verified for exit case.
12-
push ebp
13-
mov ebp, esp
14-
; callee save register
15-
push ebx
16-
push esi
17-
push edi
18-
19-
mov eax, [ebp + 0x10] ; (argc)
20-
mov eax, [ebp + 0x14] ; (argv)
21-
22-
; bx and cx are used below.
23-
mov ebx, [ebp + 0x08] ; (CS)
24-
mov ecx, [ebp + 0x0c] ; (DS)
25-
26-
mov eax, esp
27-
mov [kernel_saved_stack_top], eax
28-
29-
30-
; Preparing for exec.
31-
32-
; assigning segment registers
33-
mov es, cx
34-
mov ss, cx
35-
mov ds, cx
36-
mov fs, cx
37-
mov gs, cx
38-
39-
; assign kernel stack
40-
mov eax, 0xFFFC
41-
mov esp, eax
42-
43-
; far jump to main()
44-
push ebx ; CS, 2 bytes
45-
xor ebx, ebx
46-
push ebx ; IP: 4 bytes
47-
call far [esp]
48-
49-
; eax should contain the program return value.
50-
51-
; Returned from exec.
52-
53-
mov bx, 0x10
54-
mov es, bx
55-
mov ss, bx
56-
mov ds, bx
57-
mov fs, bx
58-
mov gs, bx
59-
60-
mov ebx, [kernel_saved_stack_top]
61-
mov esp, ebx
62-
63-
pop edi
64-
pop esi
65-
pop ebx
66-
pop ebp
67-
ret
68-
698
syscall_strncpy_user_to_kernel:
709
push ebp
7110
mov ebp, esp

0 commit comments

Comments
 (0)