Page 1 of 1

[Solved]syscall problem

Posted: Wed Jun 13, 2012 7:25 pm
by orighost
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.

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;
}

Re: syscall problem

Posted: Wed Jun 13, 2012 7:42 pm
by piranha
You should post an intelligent question. You provided us with nearly no information about your problem.

-JL

Re: syscall problem

Posted: Thu Jun 14, 2012 7:15 am
by jnc100
There are lots of errors here. For starters:

1) Where do you register an interrupt handler int the idt for int 0x80? Hint: register_interrupt_handler() does not appear to do what you think it does.
2) Why does the call to the syscall handler (in syscall_handler()) need to be done in inline assembly? Have you matched your pushes and pops? Do you intentionally clobber ebx without telling the compiler?

To be honest I haven't inspected your code in all that detail as your question does not make apparent when the int 13 exception occurs or what you have done to debug the problem.

Regards,
John.

Re: syscall problem

Posted: Fri Jun 15, 2012 7:55 am
by orighost
I have solve part problems. Now the bochs log:
00777391680i[BIOS ] int13_harddisk: function 41, unmapped device for ELDL=80
00777396461i[BIOS ] int13_harddisk: function 08, unmapped device for ELDL=80
00777401112i[BIOS ] *** int 15h function AX=00c0, BX=0000 not yet supported!
00943373577e[CPU0 ] interrupt(): soft_int && (gate.dpl < CPL)

Re: syscall problem

Posted: Fri Jun 15, 2012 8:33 am
by Combuster
...and? Obvious error is obvious.

Re: syscall problem

Posted: Thu Jun 21, 2012 6:31 am
by orighost
it's idt's issue. The idt should set ring3.