Skip to content

Commit e0cd7e5

Browse files
Bunch of code clean up, found many bugs, will patch in next commit.
1 parent 8d36e7a commit e0cd7e5

18 files changed

Lines changed: 226 additions & 260 deletions

File tree

source/compile_commands.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@
6969
"file": "/home/pradosh/osdev/source/kernel/C/user-input/ps2-mouse.c",
7070
"command": "clang -std=gnu11 -ffreestanding -w -fno-common -fno-stack-protector -fno-stack-check -fno-lto -fno-builtin -fno-omit-frame-pointer -Og -g -fPIE -I includes -I src -m64 -march=x86-64 -mno-80387 -mno-mmx -mno-red-zone -mno-sse -mno-sse2 -MMD -MP -c /home/pradosh/osdev/source/kernel/C/user-input/ps2-mouse.c -o /dev/null"
7171
},
72+
{
73+
"directory": "/home/pradosh/osdev/source",
74+
"file": "/home/pradosh/osdev/source/kernel/C/user-input/tty.c",
75+
"command": "clang -std=gnu11 -ffreestanding -w -fno-common -fno-stack-protector -fno-stack-check -fno-lto -fno-builtin -fno-omit-frame-pointer -Og -g -fPIE -I includes -I src -m64 -march=x86-64 -mno-80387 -mno-mmx -mno-red-zone -mno-sse -mno-sse2 -MMD -MP -c /home/pradosh/osdev/source/kernel/C/user-input/tty.c -o /dev/null"
76+
},
7277
{
7378
"directory": "/home/pradosh/osdev/source",
7479
"file": "/home/pradosh/osdev/source/kernel/C/logger.c",

source/includes/idt.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,17 @@
1515

1616
#include <stdint.h>
1717

18+
#define ICW1_INIT 0x10
19+
#define ICW1_ICW4 0x01
20+
#define ICW4_8086 0x01
21+
22+
#define IA32_EFER 0xC0000080
23+
#define IA32_STAR 0xC0000081
24+
#define IA32_LSTAR 0xC0000082
25+
#define IA32_FMASK 0xC0000084
26+
27+
#define EFER_SCE (1 << 0)
28+
1829
typedef struct IDTEntry
1930
{
2031
uint16_t offset_1; // offset bits 0..15
@@ -32,6 +43,18 @@ typedef struct IDTPointer
3243
uint64_t offset;
3344
}__attribute__((packed)) IDTPointer;
3445

46+
/**
47+
* @brief Located inside /interrupts/syscall-x86_64.asm
48+
*
49+
*/
50+
extern void syscall_entry(void);
51+
52+
/**
53+
* @brief Tells the CPU to use the syscall instruction.
54+
*
55+
*/
56+
void init_syscall();
57+
3558
void initIdt();
3659
void setIdtEntry(IDTEntry *target, uint64_t offset, uint16_t selector, uint8_t ist, uint8_t type_attributes);
3760

source/includes/keyboard.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ uint8_t getc();
7575
int getc_nonblock();
7676

7777
/**
78-
* @brief Non-blocking read directly from the PS/2 controller.
78+
* @brief Converts the scancode passed to it to ASCII characters.
7979
*
80-
* @return int Character if available, 0 if no input or only a modifier event
80+
* @param data Scancode
81+
* @return int eqivalent of a character.
8182
*/
82-
int kgetc_nonblock();
83-
8483
int handle_char_from_scancode(uint8_t data);
8584

8685
#endif

source/includes/meltdown.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212
#ifndef MELTDOWN_H
1313
#define MELTDOWN_H
1414

15-
#include <stddef.h>
16-
#include <stdint.h>
1715
#include <basics.h>
18-
#include <kernel.h>
19-
#include <opengl/glbackend.h>
2016
#include <graphics.h>
2117

18+
extern struct InterruptFrame;
19+
2220
/**
2321
* @brief The Meltdown (Panic) Screen
2422
*
@@ -29,6 +27,6 @@
2927
* @param cr2
3028
* @param int_no
3129
*/
32-
void meltdown_screen(cstring message, cstring file, int line, int64 error_code, int64 cr2, int64 int_no, InterruptFrame* frame);
30+
void meltdown_screen(cstring message, cstring file, int line, int64 error_code, int64 cr2, int64 int_no, struct InterruptFrame* frame);
3331

3432
#endif

source/includes/syscalls.h

Lines changed: 74 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -16,58 +16,58 @@
1616

1717
#define PRAD_MAGIC 0xBADF00D
1818

19-
#define LINUX_SYS_READ 0
20-
#define LINUX_SYS_WRITE 1
21-
#define LINUX_SYS_OPEN 2
22-
#define LINUX_SYS_CLOSE 3
23-
#define LINUX_SYS_STAT 4
24-
#define LINUX_SYS_FSTAT 5
25-
#define LINUX_SYS_LSEEK 8
26-
#define LINUX_SYS_MPROTECT 10
27-
#define LINUX_SYS_MMAP 9
28-
#define LINUX_SYS_MUNMAP 11
29-
#define LINUX_SYS_BRK 12
30-
#define LINUX_SYS_RT_SIGACTION 13
31-
#define LINUX_SYS_RT_SIGPROCMASK 14
32-
#define LINUX_SYS_IOCTL 16
33-
#define LINUX_SYS_ACCESS 21
34-
#define LINUX_SYS_WRITEV 20
35-
#define LINUX_SYS_DUP 32
36-
#define LINUX_SYS_DUP2 33
37-
#define LINUX_SYS_NANOSLEEP 35
38-
#define LINUX_SYS_GETPID 39
39-
#define LINUX_SYS_EXECVE 59
40-
#define LINUX_SYS_EXIT 60
41-
#define LINUX_SYS_CHDIR 80
42-
#define LINUX_SYS_UNAME 63
43-
#define LINUX_SYS_FCNTL 72
44-
#define LINUX_SYS_GETCWD 79
45-
#define LINUX_SYS_READLINK 89
46-
#define LINUX_SYS_UMASK 95
47-
#define LINUX_SYS_GETUID 102
48-
#define LINUX_SYS_GETEUID 107
49-
#define LINUX_SYS_GETGID 104
50-
#define LINUX_SYS_GETEGID 108
51-
#define LINUX_SYS_GETPPID 110
52-
#define LINUX_SYS_SIGALTSTACK 131
53-
#define LINUX_SYS_ARCH_PRCTL 158
54-
#define LINUX_SYS_GETTID 186
55-
#define LINUX_SYS_GETDENTS64 217
56-
#define LINUX_SYS_SET_TID_ADDRESS 218
57-
#define LINUX_SYS_CLOCK_GETTIME 228
58-
#define LINUX_SYS_OPENAT 257
59-
#define LINUX_SYS_NEWFSTATAT 262
60-
#define LINUX_SYS_READLINKAT 267
61-
#define LINUX_SYS_FACCESSAT 269
62-
#define LINUX_SYS_SET_ROBUST_LIST 273
63-
#define LINUX_SYS_PRLIMIT64 302
64-
#define LINUX_SYS_GETRANDOM 318
65-
#define LINUX_SYS_STATX 332
66-
#define LINUX_SYS_EXIT_GROUP 231
67-
#define LINUX_SYS_TGKILL 234
19+
#define LINUX_SYS_READ 0
20+
#define LINUX_SYS_WRITE 1
21+
#define LINUX_SYS_OPEN 2
22+
#define LINUX_SYS_CLOSE 3
23+
#define LINUX_SYS_STAT 4
24+
#define LINUX_SYS_FSTAT 5
25+
#define LINUX_SYS_LSEEK 8
26+
#define LINUX_SYS_MMAP 9
27+
#define LINUX_SYS_MPROTECT 10
28+
#define LINUX_SYS_MUNMAP 11
29+
#define LINUX_SYS_BRK 12
30+
#define LINUX_SYS_RT_SIGACTION 13
31+
#define LINUX_SYS_RT_SIGPROCMASK 14
32+
#define LINUX_SYS_IOCTL 16
33+
#define LINUX_SYS_ACCESS 21
34+
#define LINUX_SYS_WRITEV 20
35+
#define LINUX_SYS_DUP 32
36+
#define LINUX_SYS_DUP2 33
37+
#define LINUX_SYS_NANOSLEEP 35
38+
#define LINUX_SYS_GETPID 39
39+
#define LINUX_SYS_EXECVE 59
40+
#define LINUX_SYS_EXIT 60
41+
#define LINUX_SYS_CHDIR 80
42+
#define LINUX_SYS_UNAME 63
43+
#define LINUX_SYS_FCNTL 72
44+
#define LINUX_SYS_GETCWD 79
45+
#define LINUX_SYS_READLINK 89
46+
#define LINUX_SYS_UMASK 95
47+
#define LINUX_SYS_GETUID 102
48+
#define LINUX_SYS_GETEUID 107
49+
#define LINUX_SYS_GETGID 104
50+
#define LINUX_SYS_GETEGID 108
51+
#define LINUX_SYS_GETPPID 110
52+
#define LINUX_SYS_SIGALTSTACK 131
53+
#define LINUX_SYS_ARCH_PRCTL 158
54+
#define LINUX_SYS_GETTID 186
55+
#define LINUX_SYS_FUTEX 202
56+
#define LINUX_SYS_GETDENTS64 217
57+
#define LINUX_SYS_SET_TID_ADDRESS 218
58+
#define LINUX_SYS_CLOCK_GETTIME 228
59+
#define LINUX_SYS_OPENAT 257
60+
#define LINUX_SYS_NEWFSTATAT 262
61+
#define LINUX_SYS_READLINKAT 267
62+
#define LINUX_SYS_FACCESSAT 269
63+
#define LINUX_SYS_SET_ROBUST_LIST 273
64+
#define LINUX_SYS_PRLIMIT64 302
65+
#define LINUX_SYS_GETRANDOM 318
66+
#define LINUX_SYS_STATX 332
67+
#define LINUX_SYS_EXIT_GROUP 231
68+
#define LINUX_SYS_TGKILL 234
6869

6970
#define LINUX_EAGAIN 11
70-
#define LINUX_SYS_FUTEX 202 // confirm your syscall number
7171

7272
typedef struct syscall_frame {
7373
uint64_t r9;
@@ -86,19 +86,36 @@ typedef struct syscall_frame {
8686
} syscall_frame_t;
8787

8888
/**
89-
* @brief Prefix for the syscalls
89+
* @brief Prefix for linux-compatible syscall traces.
9090
*/
91-
#define syscalls_prefix "Syscall Invoked: "
91+
#define linux_syscalls_prefix "Linux syscall: "
9292

9393
/**
94-
* @brief Prefix for linux-compatible syscall traces.
94+
* @brief Wrapper for the dispatcher which gets called for interrupt number 0x80
95+
*
96+
* @param frame
9597
*/
96-
#define linux_syscalls_prefix "Linux syscall: "
98+
void int80_handler(InterruptFrame* frame);
9799

98-
void invoke_syscall(int64 num);
99-
void syscalls_handler(InterruptFrame* frame);
100-
void syscall_handler_syscall(syscall_frame_t* f);
100+
/**
101+
* @brief Wrapper for the dispatcher which gets called for running the syscall instruction
102+
*
103+
* @param f
104+
*/
105+
void syscall_handler(syscall_frame_t* f);
101106

107+
/**
108+
* @brief Dispatches syscall based on the syscall number.
109+
*
110+
* @param nr
111+
* @param arg1
112+
* @param arg2
113+
* @param arg3
114+
* @param arg4
115+
* @param arg5
116+
* @param arg6
117+
* @return uint64_t
118+
*/
102119
uint64_t syscall_dispatch (
103120
uint64_t nr,
104121
uint64_t arg1,

source/includes/tss.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,12 @@ extern struct tss_entry tss;
5555
*/
5656
extern uint8_t kernel_stack[0x4000];
5757

58+
/**
59+
* @brief The starting address of kernel's stack.
60+
*
61+
*/
62+
extern uint64_t kernel_stack_top;
63+
5864
/**
5965
* @brief Initialize TSS.
6066
*

source/includes/userland.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,74 @@
2626
#define USER_PHDR_VADDR (USER_TLS_VADDR + USER_TLS_REGION_SIZE)
2727
#define USER_STACK_TOP 0x00007FFFFFFFF000ULL // near top of canonical lower half
2828

29+
#define LINUX_AT_NULL 0
30+
#define LINUX_AT_PHDR 3
31+
#define LINUX_AT_PHENT 4
32+
#define LINUX_AT_PHNUM 5
33+
#define LINUX_AT_PAGESZ 6
34+
#define LINUX_AT_BASE 7
35+
#define LINUX_AT_FLAGS 8
36+
#define LINUX_AT_ENTRY 9
37+
#define LINUX_AT_UID 11
38+
#define LINUX_AT_EUID 12
39+
#define LINUX_AT_GID 13
40+
#define LINUX_AT_EGID 14
41+
#define LINUX_AT_PLATFORM 15
42+
#define LINUX_AT_HWCAP 16
43+
#define LINUX_AT_HWCAP2 26
44+
#define LINUX_AT_CLKTCK 17
45+
#define LINUX_AT_SECURE 23
46+
#define LINUX_AT_RANDOM 25
47+
#define LINUX_AT_EXECFN 31
48+
#define IA32_FS_BASE_MSR 0xC0000100
49+
#define USER_AUXV_MAX 18
50+
51+
typedef struct {
52+
int i[4];
53+
} glibc_128bits_t;
54+
55+
typedef union {
56+
uint64_t counter;
57+
struct {
58+
void* val;
59+
void* to_free;
60+
} pointer;
61+
} glibc_dtv_t;
62+
63+
typedef struct {
64+
uint64_t tcb;
65+
glibc_dtv_t* dtv;
66+
uint64_t self;
67+
uint32_t multiple_threads;
68+
uint32_t gscope_flag;
69+
uint64_t sysinfo;
70+
uint64_t stack_guard;
71+
uint64_t pointer_guard;
72+
uint64_t unused_vgetcpu_cache[2];
73+
uint32_t feature_1;
74+
int32_t __glibc_unused1;
75+
void* __private_tm[4];
76+
void* __private_ss;
77+
uint64_t ssp_base;
78+
glibc_128bits_t __glibc_unused2[8][4] __attribute__((aligned(32)));
79+
void* __padding[8];
80+
} glibc_tcb_head_t;
81+
82+
typedef struct {
83+
glibc_tcb_head_t head;
84+
glibc_dtv_t dtv[2];
85+
} glibc_tls_block_t;
86+
87+
_Static_assert(__builtin_offsetof(glibc_tcb_head_t, stack_guard) == 0x28, "glibc stack_guard offset mismatch");
88+
_Static_assert(__builtin_offsetof(glibc_tcb_head_t, pointer_guard) == 0x30, "glibc pointer_guard offset mismatch");
89+
_Static_assert(__builtin_offsetof(glibc_tcb_head_t, __private_ss) == 0x70, "glibc __private_ss offset mismatch");
90+
_Static_assert(__builtin_offsetof(glibc_tcb_head_t, __glibc_unused2) == 0x80, "glibc __glibc_unused2 offset mismatch");
91+
92+
typedef struct {
93+
uint64_t key;
94+
uint64_t value;
95+
} auxv_pair_t;
96+
2997
void enter_userland_at(uint64_t entry_point);
3098
int userland_exec(const char* path, int argc, const char* const* argv, const char* const* envp);
3199
void userland_heap_init(void);

source/kernel/C/cc-asm.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,31 @@ void set_interrupts() {
8181

8282

8383
/**
84-
* @brief It uses while loops instead of assembly's halt,
85-
* Good for Userland
84+
* @brief It uses while loops instead of assembly's halt.
8685
*
8786
*/
8887
void high_level_halt(){
8988
while(1){
90-
// TODO!
89+
asm volatile ("pause");
9190
}
91+
}
92+
93+
void wrmsr64(uint32_t msr, uint64_t value) {
94+
uint32_t low = (uint32_t)value;
95+
uint32_t high = (uint32_t)(value >> 32);
96+
asm volatile("wrmsr" : : "c"(msr), "a"(low), "d"(high));
97+
}
98+
99+
uint64_t rdmsr64(uint32_t msr) {
100+
uint32_t low = 0;
101+
uint32_t high = 0;
102+
asm volatile("rdmsr" : "=a"(low), "=d"(high) : "c"(msr));
103+
return ((uint64_t)high << 32) | low;
104+
}
105+
106+
uint64_t rdtsc64(void) {
107+
uint32_t low = 0;
108+
uint32_t high = 0;
109+
asm volatile("rdtsc" : "=a"(low), "=d"(high));
110+
return ((uint64_t)high << 32) | low;
92111
}

0 commit comments

Comments
 (0)