[Solved]syscall problem

Question about which tools to use, bugs, the best way to implement a function, etc should go here. Don't forget to see if your question is answered in the wiki first! When in doubt post here.
Post Reply
orighost
Posts: 17
Joined: Sun May 27, 2012 8:38 pm

[Solved]syscall problem

Post 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;
}
Attachments
src.tar.gz
full source code
(17.53 KiB) Downloaded 75 times
Last edited by orighost on Thu Jun 21, 2012 6:32 am, edited 3 times in total.
User avatar
piranha
Member
Member
Posts: 1391
Joined: Thu Dec 21, 2006 7:42 pm
Location: Unknown. Momentum is pretty certain, however.
Contact:

Re: syscall problem

Post by piranha »

You should post an intelligent question. You provided us with nearly no information about your problem.

-JL
SeaOS: Adding VT-x, networking, and ARM support
dbittman on IRC, @danielbittman on twitter
https://dbittman.github.io
jnc100
Member
Member
Posts: 775
Joined: Mon Apr 09, 2007 12:10 pm
Location: London, UK
Contact:

Re: syscall problem

Post 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.
orighost
Posts: 17
Joined: Sun May 27, 2012 8:38 pm

Re: syscall problem

Post 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)
User avatar
Combuster
Member
Member
Posts: 9301
Joined: Wed Oct 18, 2006 3:45 am
Libera.chat IRC: [com]buster
Location: On the balcony, where I can actually keep 1½m distance
Contact:

Re: syscall problem

Post by Combuster »

...and? Obvious error is obvious.
"Certainly avoid yourself. He is a newbie and might not realize it. You'll hate his code deeply a few years down the road." - Sortie
[ My OS ] [ VDisk/SFS ]
orighost
Posts: 17
Joined: Sun May 27, 2012 8:38 pm

Re: syscall problem

Post by orighost »

it's idt's issue. The idt should set ring3.
Post Reply