Skip to content

Commit 9de8761

Browse files
committed
Add semi-working multitasking. Colors flash on task change. Strange.
1 parent 6d641e0 commit 9de8761

File tree

20 files changed

+377
-18
lines changed

20 files changed

+377
-18
lines changed

arch/gdt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ static uint32_t lowpart = 0;
1818

1919
void gdt_init()
2020
{
21-
gdt_pointer = 0x401000; // start GDT data at 4MB
21+
gdt_pointer = 0x806; // start GDT data at 4MB
2222
mprint("location of GDT: 0x%x\n", gdt_pointer);
23-
gdtr_loc = 0x400000;
23+
gdtr_loc = 0x800;
2424
mprint("location of GDTR: 0x%x\n", gdtr_loc);
2525
gdt_add_descriptor(0, 0);
2626
gdt_add_descriptor(1, 0x00CF9A000000FFFF);

arch/idt.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ static uint8_t __idt_setup = 0;
2222

2323
void idt_init()
2424
{
25-
idt_location = 0x402000;
25+
idt_location = 0x2000;
2626
mprint("Location: 0x%x\n", idt_location);
27-
idtr_location = 0x401F00;
27+
idtr_location = 0x10F0;
2828
mprint("IDTR location: 0x%x\n", idtr_location);
2929
__idt_setup = 1;
3030
for(uint8_t i = 0; i < 255; i++)

boot.s

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ _set_gdtr:
3939
push %ebp
4040
movl %esp, %ebp
4141

42-
lgdt 0x400000
42+
lgdt 0x800
4343

4444
movl %ebp, %esp
4545
pop %ebp
@@ -51,7 +51,7 @@ _set_idtr:
5151
push %ebp
5252
movl %esp, %ebp
5353

54-
lidt 0x401F00
54+
lidt 0x10F0
5555

5656
movl %ebp, %esp
5757
pop %ebp

display/display.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <stdarg.h>
55
#include "../include/display.h"
66
#include "../include/string.h"
7+
#include "../include/mutex.h"
78
/* variables */
89
static DISPLAY dispis[DISPLAY_MAX_DISPIS];
910
static uint8_t _last_register = 1;
@@ -12,6 +13,8 @@ static uint8_t current = 0;
1213

1314
static DISPLAY* cd = 0;
1415

16+
static mutex m = { .locked = 0 };
17+
1518
char tbuf[32];
1619
char bchars[] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
1720

@@ -47,13 +50,13 @@ void __itoa_s(int i,unsigned base,char* buf) {
4750
}
4851

4952
/* abstraction methods */
50-
5153
int kprintf (const char* str, ...) {
5254
if(!str)
5355
return 0;
5456
char* s = 0;
5557
va_list ap;
5658
va_start(ap, str);
59+
//mutex_lock(&m);
5760
for(size_t i = 0; i < strlen((string)str); i++)
5861
{
5962
if(str[i] == '%')
@@ -100,6 +103,7 @@ int kprintf (const char* str, ...) {
100103
}
101104
}
102105
va_end(ap);
106+
mutex_unlock(&m);
103107
return 1;
104108
}
105109

include/memory.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33

44
#include <stdint.h>
55
#include <stddef.h>
6+
7+
extern void mm_init();
8+
9+
extern void paging_init();
10+
extern void paging_map_virtual_to_phys(uint32_t virt, uint32_t phys);
11+
12+
extern char* malloc(size_t size);
13+
614
extern void* memcpy(const void* dest, const void* src, size_t num );
715
extern void* memset (void * ptr, int value, size_t num );
816
extern void* memset16 (void *ptr, uint16_t value, size_t num);

include/mutex.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef __MUTEX_H_
2+
#define __MUTEX_H_
3+
4+
typedef struct {
5+
uint8_t locked;
6+
} mutex;
7+
8+
extern void mutex_lock(mutex* m);
9+
extern void mutex_unlock(mutex* m);
10+
11+
#endif

include/pit.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@
3131
#define PIT_OCW_COUNTER_1 0x40 //01000000
3232
#define PIT_OCW_COUNTER_2 0x80 //10000000
3333

34+
#include <stdint.h>
3435

36+
extern void set_task(uint8_t i);
3537

3638
extern void pit_init();
3739

include/tasking.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef __TASKING_H_
2+
#define __TASKING_H_
3+
4+
#include <stdint.h>
5+
6+
struct _process;
7+
8+
typedef struct _process {
9+
struct _process* prev;
10+
char* name;
11+
uint32_t pid;
12+
uint32_t esp;
13+
uint32_t eip;
14+
struct _process* next;
15+
} PROCESS;
16+
17+
extern void schedule();
18+
extern void tasking_init();
19+
20+
#endif

kernel.o

324 Bytes
Binary file not shown.

kernel/makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
all:
2-
echo -n "kernel/pic.o kernel/pit.o kernel/hal.o " >> ../objs.txt
2+
echo -n "kernel/pic.o kernel/pit.o kernel/hal.o kernel/tasking.o kernel/mutex.o " >> ../objs.txt
33
i586-elf-gcc -c pic.c -o pic.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
44
i586-elf-gcc -c pit.c -o pit.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
55
i586-elf-gcc -c hal.c -o hal.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
6+
i586-elf-gcc -c tasking.c -o tasking.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
7+
i586-elf-gcc -c mutex.c -o mutex.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra

kernel/mutex.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/** @author Levente Kurusa <[email protected]> **/
2+
#include "../include/display.h"
3+
#include "../include/mutex.h"
4+
5+
#include <stdint.h>
6+
7+
void mutex_lock(mutex* m)
8+
{
9+
/* if the lock is locked, wait and set its locked state */
10+
retest: if(m->locked) goto retest;
11+
m->locked = 1;
12+
}
13+
14+
void mutex_unlock(mutex* m)
15+
{
16+
/* this code can only be accessed by the holding thread, so unlock it */
17+
m->locked = 0;
18+
}

kernel/pit.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,30 @@
22
#include "../include/pit.h"
33
#include "../include/hal.h"
44
#include "../include/display.h"
5+
#include "../include/tasking.h"
56
#include <stdint.h>
67

78
MODULE("PIT");
89

10+
static uint8_t task = 0;
11+
12+
void set_task(uint8_t i)
13+
{
14+
task = i;
15+
}
16+
917
void pit_irq()
1018
{
11-
asm volatile("add $0x1c, %esp");
12-
asm volatile("pusha");
13-
//mprint("PIT IRQ!\n");
14-
send_eoi(0);
15-
//asm volatile("outb %%al, %%dx": :"a"(0x20),"d"(0x20));
16-
asm volatile("popa");
17-
asm volatile("iret");
19+
if(!task) {
20+
asm volatile("add $0x1c, %esp");
21+
asm volatile("pusha");
22+
send_eoi(0);
23+
asm volatile("popa");
24+
asm volatile("iret");
25+
} else {
26+
//asm volatile("add $0x1c, %esp");
27+
schedule();
28+
}
1829
}
1930

2031
static inline void __pit_send_cmd(uint8_t cmd)

kernel/tasking.c

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
/** @author Levente Kurusa <[email protected]> **/
2+
#include "../include/x86/gdt.h"
3+
#include "../include/display.h"
4+
#include "../include/pit.h"
5+
#include "../include/tasking.h"
6+
#include "../include/memory.h"
7+
8+
#include <stdint.h>
9+
10+
MODULE("TASK");
11+
12+
PROCESS* c = 0;
13+
uint32_t lpid = 0;
14+
15+
void task1()
16+
{
17+
while(1) kprintf("1");
18+
}
19+
20+
void task2()
21+
{
22+
while(1) kprintf("2");
23+
}
24+
25+
void idle_thread()
26+
{
27+
while(1);
28+
}
29+
30+
void kill(uint32_t pid)
31+
{
32+
if(pid == 1) panic("Idle can't be killed!\n");
33+
}
34+
35+
PROCESS* createProcess(char* name, uint32_t addr)
36+
{
37+
PROCESS* p = malloc(sizeof(PROCESS));
38+
memset(p, 0, sizeof(PROCESS));
39+
p->name = name;
40+
p->pid = ++lpid;
41+
p->eip = addr;
42+
p->esp = (uint32_t)malloc(4096);
43+
uint32_t* stack = p->esp + 4096;
44+
*--stack = 0x00000202; // eflags
45+
*--stack = 0x8; // cs
46+
*--stack = (uint32_t)addr; // eip
47+
*--stack = 0; // eax
48+
*--stack = 0; // ebx
49+
*--stack = 0; // ecx;
50+
*--stack = 0; //edx
51+
*--stack = 0; //esi
52+
*--stack = 0; //edi
53+
*--stack = p->esp + 4096; //ebp
54+
*--stack = 0x10; // ds
55+
*--stack = 0x10; // fs
56+
*--stack = 0x10; // es
57+
*--stack = 0x10; // gs
58+
p->esp = (uint32_t)stack;
59+
mprint("Created task %s with esp=0x%x eip=0x%x\n", p->name, p->esp, p->eip);
60+
return p;
61+
}
62+
63+
void addProcess(PROCESS* p)
64+
{
65+
set_task(0);
66+
p->next = c->next;
67+
p->next->prev = p;
68+
p->prev = c;
69+
c->next = p;
70+
set_task(1);
71+
}
72+
73+
void schedule()
74+
{
75+
//while(1);
76+
asm volatile("push %eax");
77+
asm volatile("push %ebx");
78+
asm volatile("push %ecx");
79+
asm volatile("push %edx");
80+
asm volatile("push %esi");
81+
asm volatile("push %edi");
82+
asm volatile("push %ebp");
83+
asm volatile("push %ds");
84+
asm volatile("push %es");
85+
asm volatile("push %fs");
86+
asm volatile("push %gs");
87+
asm volatile("mov %%esp, %%eax":"=a"(c->esp));
88+
c = c->next;
89+
asm volatile("mov %%eax, %%esp": :"a"(c->esp));
90+
asm volatile("pop %gs");
91+
asm volatile("pop %fs");
92+
asm volatile("pop %es");
93+
asm volatile("pop %ds");
94+
asm volatile("pop %ebp");
95+
asm volatile("pop %edi");
96+
asm volatile("pop %esi");
97+
asm volatile("pop %edx");
98+
asm volatile("pop %ecx");
99+
asm volatile("pop %ebx");
100+
asm volatile("pop %eax");
101+
asm volatile("out %%al, %%dx": :"d"(0x20), "a"(0x20));
102+
asm volatile("iret");
103+
}
104+
105+
void tasking_init()
106+
{
107+
mprint("Creating idle process\n");
108+
c = createProcess("kidle", (uint32_t)idle_thread);
109+
c->next = c;
110+
c->prev = c;
111+
addProcess(createProcess("task1", (uint32_t)task1));
112+
addProcess(createProcess("task2", (uint32_t)task2));
113+
mprint("Tasking online!\n");
114+
}

linker.ld

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ ENTRY(_start)
22
SECTIONS
33
{
44
. = 1M;
5+
kernel_base = .;
56
.text BLOCK(4K) : ALIGN(4K)
67
{
78
*(.multiboot)
@@ -21,4 +22,5 @@ SECTIONS
2122
*(.bss)
2223
*(.bootstrap_stack)
2324
}
25+
kernel_end = .;
2426
}

main.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,15 @@
1414
#include "include/pit.h"
1515
#include "include/pic.h"
1616
#include "include/hal.h"
17+
#include "include/tasking.h"
1718

1819
static DISPLAY* disp = 0;
1920

2021
MODULE("MAIN");
2122

23+
extern void kernel_end;
24+
extern void kernel_base;
25+
2226
#if defined(__cplusplus)
2327
extern "C" /* ha C++ compiler, akkor ez a függvény legyen C99es linkage-ű. */
2428
#endif
@@ -29,7 +33,10 @@ void kernel_main()
2933
/* We create a textmode display driver, register it, then set it as current */
3034
display_setcurrent(display_register(textmode_init()));
3135
mprint("Welcome to LevOS 4.0, very unstable!\n");
36+
mprint("Kernel base is 0x%x, end is 0x%x\n", &kernel_base, &kernel_end);
3237
if(eax == 0x1337) mprint("Assembly link established.\n");
38+
/* Setup memory manager */
39+
mm_init(&kernel_end);
3340
/* So far good, we have a display running,
3441
* but we are *very* early, set up arch-specific
3542
* tables, timers and memory to continue to tasking.
@@ -44,9 +51,10 @@ void kernel_main()
4451
/* Enabling interrupts early... */
4552
asm volatile("sti");
4653
mprint("Interrupts were enabled, beware.\n");
47-
/* Setup memory manager */
4854
/* Setup paging. */
55+
paging_init();
4956
/* Enable tasking. */
57+
tasking_init();
5058
#ifdef __cplusplus
5159
mprint("C++ version, may cause malfunction!\n");
5260
#endif

makefile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ compile: assembly bkernel link
22
all: assembly bkernel link start
33

44
start:
5-
qemu -kernel levos.bin
5+
qemu -kernel levos.bin -monitor /dev/stdout
6+
reset
67

78
assembly:
89
i586-elf-as boot.s -o boot.o

memory/makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
all:
2-
echo -n "memory/memutils.o " >> ../objs.txt
2+
echo -n "memory/memutils.o memory/malloc.o memory/paging.o " >> ../objs.txt
33
i586-elf-gcc -c memutils.c -o memutils.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
4+
i586-elf-gcc -c malloc.c -o malloc.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra
5+
i586-elf-gcc -c paging.c -o paging.o -std=gnu99 -ffreestanding -O2 -Wall -Wextra

0 commit comments

Comments
 (0)