[Solved]syscall problem
Posted: Wed Jun 13, 2012 7:25 pm
I follow "JamesM's kernel development tutorials" to realize system call. But when I run it, throw an exception (isr13).
To simple I has set all pages on usre mode, and I have already switch to user mode. What should I do?
Follow code maybe useful.
To simple I has set all pages on usre mode, and I have already switch to user mode. What should I do?
Follow code maybe useful.
Code: Select all
#define DEFN_SYSCALL1(fn,num,P1) \
int syscall_##fn(P1 p1) \
{ \
int a; \
asm volatile("int $0x80" : "=a"(a) : "0"(num), "b"((int)p1)); \
return a; \
}
Code: Select all
DEFN_SYSCALL1(monitor_write, 0, char*);
static void *syscalls[3] =
{
&monitor_write,
&monitor_write_hex,
&monitor_write_dec,
};
uint32_t num_syscalls = 3;
void initialize_syscalls()
{
register_interrupt_handler(0x80, &syscall_handler);
}
void syscall_handler(registers_t *regs)
{
if(regs->eax >= num_syscalls)
return;
void *location = syscalls[regs->eax];
int ret;
asm volatile (" \
push %1; \
push %2; \
push %3; \
push %4; \
push %5; \
call *%6; \
pop %%ebx; \
pop %%ebx; \
pop %%ebx; \
pop %%ebx; \
pop %%ebx; \
" : "=a"(ret) : "r"(regs->edi), "r" (regs->esi), "r" (regs->edx), "r" (regs->ecx), "r" (regs->ebx), "r" (location));
regs->eax = ret;
}
Code: Select all
void switch_to_user_modes()
{
uint32_t esp;
asm volatile("mov %%esp, %0":"=r"(esp));
set_kernel_stack(esp);
//monitor_write_hex(cs);
// Set up our kernel stack.
//set_kernel_stack(current_task->kernel_stack+KERNEL_STACK_SIZE);
// Set up a stack structure for switching to user mode.
asm volatile(" \
cli; \
mov $0x23, %ax; \
mov %ax, %ds; \
mov %ax, %es; \
mov %ax, %fs; \
mov %ax, %gs; \
\
\
mov %esp, %eax; \
pushl $0x23; \
pushl %eax; \
pushf; \
pushl $0x1B; \
push $1f; \
iret; \
1: \
");
//enable_interrupts();
}
Code: Select all
int kernel_main(uint32_t magic, multiboot_t *mboot_ptr)
{
init_gdt();
init_idt();
init_elf(mboot_ptr);
printk_clear();
//asm volatile("int $0x3");
//init_timer(1);
//0x100000 mboot_ptr->mem_upper
init_pmm(0x400000);
init_vmm();
init_heap();
init_memory(mboot_ptr);
asm volatile("sti");
switch_to_user_modes();
syscall_monitor_write("Hello, user world!\n");
return 0xDEADBEEF;
}