Skip to content

Commit 63e1b87

Browse files
committed
Context switch to one thread implemented
1 parent b7165e3 commit 63e1b87

File tree

6 files changed

+103
-4
lines changed

6 files changed

+103
-4
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ obj/*
88
kernel
99
*.img
1010
*.iso
11+
.gdb_history

include/sys/task.h

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#ifndef _TASK_H
2+
#define _TASK_H
3+
typedef struct _regstruct
4+
{
5+
uint64_t rbp;
6+
uint64_t rax;
7+
uint64_t rbx;
8+
uint64_t rcx;
9+
uint64_t rdx;
10+
uint64_t rsp;
11+
uint64_t flags;
12+
} __attribute__((packed)) reg_struct;
13+
14+
typedef struct _taskstruct
15+
{
16+
reg_struct regs;
17+
} __attribute__((packed)) task_struct;
18+
void trial_sched();
19+
#endif

sys/main.c

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <sys/kprintf.h>
88
#include <sys/pci.h>
99
#include <sys/tarfs.h>
10+
#include <sys/task.h>
1011
#include <sys/vma.h>
1112

1213
#define INITIAL_STACK_SIZE 4096
@@ -49,6 +50,8 @@ start(uint32_t* modulep, void* physbase, void* physfree)
4950
vma_pagelist_add_addresses(smap->base, smap->base + smap->length);
5051
}
5152
}
53+
kprintf("physfree %p\n", (uint64_t)physfree);
54+
kprintf("tarfs in [%p:%p]\n", &_binary_tarfs_start, &_binary_tarfs_end);
5255
vma_pagelist_create(physfree);
5356
vma_create_pagetables();
5457
kprintf("physfree %p\n", (uint64_t)physfree);
@@ -57,6 +60,7 @@ start(uint32_t* modulep, void* physbase, void* physfree)
5760
register_idt();
5861
pic_init();
5962
enable_interrupts(TRUE);
63+
trial_sched();
6064
/*
6165
ahci_discovery();
6266
ahci_readwrite_test();

sys/sched.s

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
.globl switch_to
2+
switch_to:
3+
// rdi: me
4+
// rsi: next
5+
movq %rbp, 0(%rdi)
6+
movq %rax, 8(%rdi)
7+
movq %rbx, 16(%rdi)
8+
movq %rcx, 24(%rdi)
9+
movq %rdx, 32(%rdi)
10+
11+
pushf
12+
pop 48(%rdi)
13+
14+
push %rdi
15+
movq %rsp, 40(%rdi)
16+
17+
movq 40(%rsi), %rsp
18+
pop %rdi
19+
20+
movq 0(%rdi), %rbp
21+
movq 8(%rdi), %rax
22+
movq 16(%rdi), %rbx
23+
movq 24(%rdi), %rcx
24+
movq 32(%rdi), %rdx
25+
push 48(%rdi)
26+
popf
27+
28+
retq
29+
.end

sys/task.c

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#include <sys/defs.h>
2+
#include <sys/kprintf.h>
3+
#include <sys/task.h>
4+
5+
extern void switch_to(task_struct*, task_struct*);
6+
uint8_t second_stack[4096] __attribute__((aligned(16)));
7+
task_struct tasks[2];
8+
task_struct *me, *next;
9+
10+
void
11+
yield()
12+
{
13+
switch_to(me, next);
14+
}
15+
16+
void
17+
thread2()
18+
{
19+
kprintf("Thread X! Hello from the other side\n");
20+
while (1)
21+
;
22+
yield();
23+
}
24+
25+
void
26+
trial_sched()
27+
{
28+
me = &tasks[0];
29+
next = &tasks[1];
30+
uint64_t* stack_top;
31+
stack_top = (uint64_t*)&second_stack[4096];
32+
stack_top--;
33+
stack_top--;
34+
stack_top--;
35+
stack_top--;
36+
stack_top--;
37+
stack_top--;
38+
*stack_top = (uint64_t)thread2;
39+
stack_top--;
40+
*stack_top = (uint64_t)next;
41+
next->regs.rsp = (uint64_t)stack_top;
42+
next->regs.rbp = (uint64_t)stack_top;
43+
yield();
44+
kprintf("Thread 1! Hello from the other side\n");
45+
}

sys/vma.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#define VMA_KERNMEM 0xffffffff80000000
55
#define VMA_VIDEO (VMA_KERNMEM + 0x800000) // 2nd part is 2048 * PAGESIZE
6-
#define PAGELIST_ENTRIES (1024 * 256)
6+
#define PAGELIST_ENTRIES (1024 * 1024)
77
#define PAGE_SIZE 4096
88
#define TABLE_ENTRIES 512
99

@@ -99,8 +99,8 @@ vma_create_pagetables()
9999
// Self referencing trick
100100
pml4_table[TABLE_ENTRIES - 1] = ((uint64_t)pml4_table) | 0x3;
101101

102-
//TODO Remove this hard coding of 2048. Map only physbase to physfree
103-
for (int i = 0; i < 2048; i++) {
102+
// TODO Remove this hard coding of 2048. Map only physbase to physfree
103+
for (int i = 0; i < 4735; i++) {
104104
v_addr = VMA_KERNMEM + i * (PAGE_SIZE);
105105
pdp_table = vma_add_table_mapping(pml4_table, VMA_PML4_OFFSET(v_addr));
106106
pd_table =
@@ -111,7 +111,8 @@ vma_create_pagetables()
111111
pt_table[VMA_PAGE_TABLE_OFFSET(v_addr)] = (0x0 + i * (PAGE_SIZE)) | 0x3;
112112
}
113113

114-
//TODO add new function to create the mapping making the following code generic
114+
// TODO add new function to create the mapping making the following code
115+
// generic
115116
v_addr = VMA_VIDEO;
116117
pdp_table = vma_add_table_mapping(pml4_table, VMA_PML4_OFFSET(v_addr));
117118
pd_table = vma_add_table_mapping(pdp_table, VMA_PD_POINTER_OFFSET(v_addr));

0 commit comments

Comments
 (0)