Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Kernel/Kernel.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,6 @@ void task2(void) {
return;
}
void task3(void) {
// Test syscall
asm volatile(
"mov $2, %%rax\n" // SYS_WRITE
"mov $1, %%rbx\n" // stdout
Expand All @@ -132,7 +131,7 @@ void task3(void) {
: "r"("Syscall test!")
: "rax", "rbx", "rcx", "r8"
);

while (1) {
PrintKernelAt("T3 syscall", 12, 0);
for(volatile int i = 0; i < 50000; i++);
Expand Down
9 changes: 6 additions & 3 deletions Kernel/Syscall.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
#include "Idt.h"
extern void SyscallEntry(void);
uint64_t SyscallHandler(uint64_t syscall_num, uint64_t arg1, uint64_t arg2, uint64_t arg3) {
Process* current = GetCurrentProcess();
switch (syscall_num) {
case SYS_EXIT:
// Terminate current process
GetCurrentProcess()->state = PROC_TERMINATED;
if (current) {
current->state = PROC_TERMINATED;
}
Schedule(); // Switch to next process
return 0;

Expand All @@ -32,7 +35,7 @@ uint64_t SyscallHandler(uint64_t syscall_num, uint64_t arg1, uint64_t arg2, uint
return 0;

case SYS_GETPID:
return GetCurrentProcess()->pid;
return current ? current->pid : -1;

default:
return -1;
Expand All @@ -41,5 +44,5 @@ uint64_t SyscallHandler(uint64_t syscall_num, uint64_t arg1, uint64_t arg2, uint

void SyscallInit(void) {
// Install syscall interrupt (0x80)
IdtSetGate(0x80, (uint64_t)SyscallEntry, 0x08, 0x8E);
IdtSetGate(0x80, (uint64_t)SyscallEntry, SYSCALL_INTERRUPT_VECTOR, IDT_INTERRUPT_GATE_KERNEL);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Verification agent

🧩 Analysis chain

Verify the symbolic constant usage is correct.

The use of symbolic constants is excellent for maintainability. However, there's a naming issue with SYSCALL_INTERRUPT_VECTOR - it's set to 0x08 which is actually the kernel code segment selector, not an interrupt vector.

Based on the retrieved learnings, the IdtSetGate parameters are correct:

  • 0x80 (interrupt vector)
  • SyscallEntry (handler address)
  • SYSCALL_INTERRUPT_VECTOR (0x08 - kernel code selector)
  • IDT_INTERRUPT_GATE_KERNEL (0x8E - gate flags)

The constant naming could be clearer. Please verify that the syscall setup works correctly with these constants:


🏁 Script executed:

#!/bin/bash
# Description: Verify syscall interrupt vector setup and usage
# Expected: Find consistent usage of 0x80 as interrupt vector and 0x08 as selector

echo "=== Searching for syscall interrupt vector usage ==="
rg -A 2 -B 2 "0x80|syscall.*interrupt" 

echo "=== Searching for IdtSetGate function calls ==="
rg -A 2 -B 2 "IdtSetGate"

echo "=== Searching for symbolic constants usage ==="
rg -A 2 -B 2 "SYSCALL_INTERRUPT_VECTOR|IDT_INTERRUPT_GATE_KERNEL"

Length of output: 21978


Rename SYSCALL_INTERRUPT_VECTOR to clarify its role as a segment selector

The parameters to IdtSetGate are:

  • 0x80 = interrupt vector
  • SyscallEntry = handler address
  • 0x08 = segment selector, not an interrupt vector
  • 0x8E = gate flags

Everything is wired up correctly, but the constant name is misleading. Please rename it and update its use:

  • Kernel/Syscall.h
-#define SYSCALL_INTERRUPT_VECTOR 0x08
+#define SYSCALL_SEGMENT_SELECTOR  0x08
  • Kernel/Syscall.c
-    IdtSetGate(0x80, (uint64_t)SyscallEntry, SYSCALL_INTERRUPT_VECTOR, IDT_INTERRUPT_GATE_KERNEL);
+    IdtSetGate(0x80, (uint64_t)SyscallEntry, SYSCALL_SEGMENT_SELECTOR,  IDT_INTERRUPT_GATE_KERNEL);
🤖 Prompt for AI Agents
In Kernel/Syscall.c at line 47 and Kernel/Syscall.h, rename the constant
SYSCALL_INTERRUPT_VECTOR to a name that clearly indicates it is a segment
selector, such as SYSCALL_SEGMENT_SELECTOR. Update all references to this
constant in both files accordingly to reflect its true purpose as a segment
selector rather than an interrupt vector.

}
3 changes: 2 additions & 1 deletion Kernel/Syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
#define SYS_WRITE 2
#define SYS_READ 3
#define SYS_GETPID 4

#define SYSCALL_INTERRUPT_VECTOR 0x08
#define IDT_INTERRUPT_GATE_KERNEL 0x8E
// System call handler
void SyscallInit(void);
uint64_t SyscallHandler(uint64_t syscall_num, uint64_t arg1, uint64_t arg2, uint64_t arg3);
Expand Down
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,5 @@ iso = custom_target('VoidFrame.iso',

# Run target
run_target('run',
command : ['qemu-system-x86_64', '-cdrom', iso, '-m', '1G']
command : ['qemu-system-x86_64', '-cdrom', 'VoidFrame.iso', '-m', '1G']
)