Skip to content

Commit 2641f11

Browse files
committed
Fixed the kernel threads execution
1 parent 0ac9fd2 commit 2641f11

35 files changed

+630
-243
lines changed

bochsrc.txt

+51-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,53 @@
1-
# bochsrc
2-
#
1+
# configuration file generated by Bochs
2+
plugin_ctrl: usb_uhci=false, serial=true, speaker=true, unmapped=true, parallel=true, biosdev=true, iodebug=true, extfpuirq=true
3+
config_interface: textconfig
34
display_library: sdl2
4-
megs: 64
5-
#
6-
mouse: enabled=0
7-
#
8-
# ata0-master: type=disk, path="menios.hdd", mode=flat, cylinders=609, heads=16, spt=64
9-
ata0-master: type=disk, path="menios.hdd", mode=flat, cylinders=16384, heads=16, spt=63
10-
#
11-
log: bochsout.txt
12-
#
5+
memory: guest=64, host=64, block_size=128
6+
romimage: file="/opt/homebrew/Cellar/bochs/2.8/share/bochs/BIOS-bochs-latest", address=0x00000000, options=none, flash_data=none
7+
vgaromimage: file="/opt/homebrew/Cellar/bochs/2.8/share/bochs/VGABIOS-lgpl-latest"
138
boot: disk
9+
floppy_bootsig_check: disabled=0
10+
floppya: type=1_44
11+
# no floppyb
12+
ata0: enabled=true, ioaddr1=0x1f0, ioaddr2=0x3f0, irq=14
13+
ata0-master: type=disk, path="menios.hdd", mode=flat, cylinders=1, heads=1, spt=1, sect_size=512, model="Generic 1234", biosdetect=auto, translation=auto
14+
ata0-slave: type=none
15+
ata1: enabled=true, ioaddr1=0x170, ioaddr2=0x370, irq=15
16+
ata1-master: type=none
17+
ata1-slave: type=none
18+
ata2: enabled=false
19+
ata3: enabled=false
20+
optromimage1: file=none
21+
optromimage2: file=none
22+
optromimage3: file=none
23+
optromimage4: file=none
24+
optramimage1: file=none
25+
optramimage2: file=none
26+
optramimage3: file=none
27+
optramimage4: file=none
28+
pci: enabled=1, chipset=i440fx, slot1=none, slot2=none, slot3=none, slot4=none, slot5=none
29+
vga: extension=vbe, update_freq=10, realtime=1, ddc=builtin
30+
cpu: count=1, ips=4000000, model=corei7_icelake_u, reset_on_triple_fault=1, cpuid_limit_winnt=0, ignore_bad_msrs=1, mwait_is_nop=0
31+
print_timestamps: enabled=0
32+
debugger_log: -
33+
magic_break: enabled=1 0x0
34+
port_e9_hack: enabled=false, all_rings=false
35+
iodebug: all_rings=0
36+
private_colormap: enabled=0
37+
clock: sync=none, time0=utc, rtc_sync=0
38+
# no cmosimage
39+
log: bochsout.txt
40+
logprefix: %t%e%d
41+
debug: action=ignore
42+
info: action=report
43+
error: action=report
44+
panic: action=ask
45+
keyboard: type=mf, serial_delay=150, paste_delay=100000, user_shortcut=none
46+
mouse: type=ps2, enabled=false, toggle=ctrl+mbutton
47+
com1: enabled=true, mode=file, dev="com1bochs.log"
48+
com2: enabled=false
49+
com3: enabled=false
50+
com4: enabled=false
51+
speaker: enabled=true, mode=gui
52+
parport1: enabled=true, file=none
53+
parport2: enabled=false

include/kernel/heap.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@ extern "C" {
77

88
#include <types.h>
99

10-
#define HEAP_SIZE 256
10+
#define HEAP_SIZE 0x400
1111
#define HEAP_FREE 0
1212
#define HEAP_USED 1
13-
#define HEAP_MAGIC 0x534f6d00
13+
#define HEAP_MAGIC 0x534f6d00 // mOS
1414

1515
typedef uint32_t HEAP_INSPECT_RESULT;
1616

@@ -25,7 +25,6 @@ typedef struct heap_node_t {
2525
uint8_t status; // 1 byte
2626
uint32_t size; // 4 bytes
2727
struct heap_node_t* next; // 8 bytes
28-
struct heap_node_t* prev; // 8 bytes
2928
uint8_t data[];
3029
} heap_node_t;
3130

@@ -45,6 +44,8 @@ void kfree(void* ptr);
4544

4645
void dump_heap(heap_node_p heap, size_t size);
4746

47+
void heap_compactor();
48+
4849
#ifdef __cplusplus
4950
}
5051
#endif

include/kernel/kernel.h

+1
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ void outb(uint16_t port, uint8_t value);
1313
void outw(uint16_t port, uint16_t value);
1414
void outl(uint16_t port, uint32_t value);
1515
void sti();
16+
void noop();
1617

1718
#endif

include/kernel/mem.h

+1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
#include <types.h>
55

66
void mem_init();
7+
void init_memory_compactor();
78

89
#endif

include/kernel/mutex.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef MENIOS_INCLUDE_KERNEL_MUTEX_H
2+
#define MENIOS_INCLUDE_KERNEL_MUTEX_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
typedef struct kthread_mutex_t {
9+
int lock;
10+
uint32_t pid;
11+
} kmutex_t;
12+
13+
int kmutex_lock(kmutex_t* mutex);
14+
int kmutex_unlock(kmutex_t* mutex);
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
#endif

include/kernel/proc.h

+20-18
Original file line numberDiff line numberDiff line change
@@ -42,27 +42,26 @@ extern "C" {
4242
#define PROC_STACK_SIZE 1024
4343

4444
typedef struct cpu_state_t {
45-
uint64_t rax;
46-
uint64_t rbx;
47-
uint64_t rcx;
48-
uint64_t rdx;
49-
uint64_t rsi;
45+
uint64_t r15;
46+
uint64_t r14;
47+
uint64_t r13;
48+
uint64_t r12;
49+
uint64_t r11;
50+
uint64_t r10;
51+
uint64_t r9;
52+
uint64_t r8;
5053
uint64_t rdi;
54+
uint64_t rsi;
5155
uint64_t rbp;
52-
uint64_t rsp;
53-
uint64_t r8;
54-
uint64_t r9;
55-
uint64_t r10;
56-
uint64_t r11;
57-
uint64_t r12;
58-
uint64_t r13;
59-
uint64_t r14;
60-
uint64_t r15;
61-
// phys_addr_t cr3;
56+
uint64_t rdx;
57+
uint64_t rcx;
58+
uint64_t rbx;
59+
uint64_t rax;
6260
uint64_t rip;
63-
uint64_t cs;
61+
uint64_t cs;
6462
uint64_t rflags;
65-
uint64_t ss;
63+
uint64_t rsp;
64+
uint64_t ss;
6665
} __attribute__((packed)) cpu_state_t;
6766

6867
typedef cpu_state_t* cpu_state_p;
@@ -79,13 +78,15 @@ typedef struct proc_info_t {
7978
uint32_t pid;
8079
uintptr_t brk;
8180
uintptr_t heap;
82-
uintptr_t* stack;
81+
void* stack_pointer;
82+
uintptr_t* stack_base;
8383
proc_state_t state;
8484
uint8_t priority;
8585
cpu_state_t* cpu_state;
8686
void(*entrypoint)(void*);
8787
void* arguments;
8888
proc_info_p next;
89+
int exit_code;
8990
char name[32];
9091
} proc_info_t;
9192

@@ -97,6 +98,7 @@ extern proc_info_p current;
9798
void init_scheduler();
9899
void proc_create(proc_info_p proc, const char* name, void (*entrypoint)(void *), void* arg);
99100
void proc_execute(proc_info_p proc);
101+
void proc_exit(int code);
100102

101103
#ifdef __cplusplus
102104
}

include/kernel/serial.h

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define MENIOS_INCLUDE_KERNEL_SERIAL_H
33

44
#include <stdarg.h>
5+
#include <stdbool.h>
56

67
#ifdef MENIOS_KERNEL
78
void serial_init();
@@ -39,6 +40,7 @@ int serial_vprintf(const char *format, va_list args);
3940
#endif // MENIOS_NO_DEBUG
4041
#endif // MENIOS_KERNEL
4142

43+
extern bool serial_debug;
4244
// #define serial_line(a
4345
// #define serial_log(a) serial_printf("[INFO] %s[%d]: %s\n", __FILE__, __LINE__, a)
4446
// #define serial_error(a) serial_printf("[ERRO] %s[%d]: %s\n", __FILE__, __LINE__, a)

include/kernel/services.h

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
void init_services();

include/kernel/thread.h

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,19 @@ extern "C" {
66
#endif
77

88
typedef struct kthread_t {
9-
char* name;
9+
const char* name;
10+
void (*entrypoint)(void*);
11+
void* arguments;
1012
} kthread_t;
1113

14+
typedef kthread_t* kthread_p;
15+
1216
// typedef void *(*entrypoint)(void *) kthread_handler_t;
1317

14-
int kthread_create(kthread_t* thread, void (*entrypoint)(void *), void* arg);
18+
int kthread_create(kthread_t* thread, const char* name, void (*entrypoint)(void *), void* arg);
19+
20+
void ksleep(uint64_t milliseconds);
21+
void kexit(int code);
1522

1623
#ifdef __cplusplus
1724
}

include/kernel/timer.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern "C" {
1010
void timer_init();
1111
void timer_eoi();
1212

13-
uint64_t unix_time();
13+
uint64_t boot_time();
1414

1515
void show_clock(void*);
1616

include/kernel/tsc.h

+5
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
extern "C" {
66
#endif
77

8+
#include <time.h>
89
#include <types.h>
910

11+
#define TICKS_PER_SECOND 1000000000
12+
1013
void init_tsc();
1114

1215
uint64_t read_tsc(void);
1316

17+
useconds_t unix_time_us();
18+
1419
#ifdef __cplusplus
1520
}
1621
#endif

include/sys/errno.h

+2
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ extern "C" {
130130
#define ERESTARTSYS 512
131131
#define ERESTARTNOINTR 513
132132

133+
extern int errno;
134+
133135
#ifdef __cplusplus
134136
}
135137
#endif

include/sys/mman.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef MENIOS_INCLUDE_SYS_MMAN_H
2+
#define MENIOS_INCLUDE_SYS_MMAN_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
#include <stddef.h> // size_t
9+
#include <types.h>
10+
11+
#define PROT_NONE 0x00 // no access
12+
#define PROT_READ 0x01 // pages can be read
13+
#define PROT_WRITE 0x02 // pages can be written
14+
#define PROT_EXEC 0x04 // pages can be executed
15+
16+
#define MAP_SHARED 0x0001
17+
#define MAP_PRIVATE 0x0002 // changes are private
18+
#define MAP_ANON 0x1000 // allocated from memory
19+
#define MAP_ANONYMOUS MAP_ANON // alternate POSIX spelling
20+
21+
#define MAP_FAILED ((void *) -1) // mapping failed
22+
23+
void* mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
24+
int munmap(void *addr, size_t len);
25+
26+
#ifdef __cplusplus
27+
}
28+
#endif
29+
#endif //MENIOS_INCLUDE_SYS_MMAN_H

include/sys/types.h

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#ifndef MENIOS_INCLUDE_SYS_TYPES_H
2+
#define MENIOS_INCLUDE_SYS_TYPES_H
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
typedef long long int useconds_t;
9+
typedef long long int time_t;
10+
11+
#ifdef __cplusplus
12+
}
13+
#endif
14+
15+
#endif // MENIOS_INCLUDE_SYS_TYPES_H

include/time.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef INCLUDE_TIME_H
2+
#define INCLUDE_TIME_H
3+
4+
#include <sys/types.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
#ifdef __cplusplus
11+
}
12+
#endif
13+
14+
#endif /* INCLUDE_TIME_H */

0 commit comments

Comments
 (0)