Communication between kernel and application

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
hunter

Communication between kernel and application

Post by hunter »

Hello,

I've a strange problem in my OS ...
The communication between the kernel and the applications works with interrupts and the registers.

So if I want to send the kernel numbers it works fine here is an example:

Code: Select all

int main( void )
{
???unsigned int num1 = 100;
???unsigned int num2 = 200;

???asm("movl %0, %%ebx" :: "r" (num1));
???asm("movl %0, %%ecx" :: "r" (num2));
???asm("movl $0x0001, %eax");????????????// Funktionsnummer
???asm("int $0x30");??????????????????// Systeminterrupt

???return 0;
}
The kernel gets the numbers and can work with them.

But if I want to send the kernel strings, there is a big problem and I can't find the mistake ... here is an other example:

Code: Select all

int main( void )
{
???unsigned char text[] = "Test Test Test";

???asm("movl %0, %%ebx" :: "r" (text));
???asm("movl $0x0002, %eax");????????????// Functionsnumber
???asm("int $0x30");??????????????????// Systeminterrupt

???return 0;
}
in the kernel the EBX register contains strange things ... i hope somebody could help me ...
User avatar
Solar
Member
Member
Posts: 7635
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Communication between kernel and application

Post by Solar »

hunter wrote: in the kernel the EBX register contains strange things ...
Like, a pointer to the address where - in the address space of the application - the string is located?
Every good solution is obvious once you've found it.
Kemp

Re:Communication between kernel and application

Post by Kemp »

Yup, plus that wouldn't work even if it tried to magically insert the string, unless EBX happened to be 14 bytes long...
hunter

Re:Communication between kernel and application

Post by hunter »

But some times ago, the variant to put the pointer of the String into EBX and send it to the kernel, works fine ...

@Kemp: Could you tell me how I can send strings from the application to the kernel ... it doesn't work if i put the pointer in EBX and send it to the kernel ...
ineo

Re:Communication between kernel and application

Post by ineo »

What they mean is that in EBX you only have the adress of your string, but that address is mapped in your userspace, and probably not in kernel space.

Therefore it has no meaning in kernel space.

You could try the following: map your process userspace in kernel adress space and convert the adress to get the string. But this is mostly a hack, you should think of the way you want your IPC to work.
Read documentation on IPC to see the differnt possibilities you have for messaging ;)
User avatar
spix
Member
Member
Posts: 128
Joined: Mon Jun 26, 2006 8:41 am
Location: Millicent, South Australia
Contact:

Re:Communication between kernel and application

Post by spix »

I use the following in my write syscall

Code: Select all

int _write(int file, char *ptr, int len) {
   int ret;
   __asm__ volatile("int $0x30" : "=a" (ret) : "0" (SYS_WRITE), "b" (file), "c" (ptr), "d" (len));
   return ret;
}
and this on the syscall end:

Code: Select all

      case 5:
         r->eax = sys_write(r->ebx, (char *)r->ecx, r->edx);
         break;
and strings get passed fine between user land and kernel land, but my segments for userspace and kernelspace both cover the whole address space, and the same page directory is used..

Does your code have paging enabled?
durand
Member
Member
Posts: 193
Joined: Wed Dec 21, 2005 12:00 am
Location: South Africa
Contact:

Re:Communication between kernel and application

Post by durand »

Be careful of the gnu assembler syntax you're using.

Code: Select all

   asm("movl %0, %%ebx" :: "r" (num1));
   asm("movl %0, %%ecx" :: "r" (num2));
   asm("movl $0x0001, %eax");            // Funktionsnummer
   asm("int $0x30");                  // Systeminterrupt
The code you're using clobbers some registers but does not restore them. You could be confusing the compiler, introducing really unusual bugs later on. Why not something like:

Code: Select all

   asm(  "movl %0, %%ebx;"
            "movl %1, %%ecx;"
            "movl $0x0001, %%eax;"
            "int $0x30;"
            :
            : "r" (num1), "r" (num2)
            : "eax", "ebx", "ecx"
           );                
The last line ( : "eax", "ebx", "ecx" ) tells GCC which registers you clobbered and lets it compensate or be aware of your actions.
hunter

Re:Communication between kernel and application

Post by hunter »

Thanks ...

@spix:
Some times ago I tried your methode ... but there is the same result as in my old methode.

@durand:
Thanks, but if I use you methode there iis the same result, there is no string send to the kernel ...
hunter

Re:Communication between kernel and application

Post by hunter »

Sorry I have forgotten to say that I use Paging ...
User avatar
Solar
Member
Member
Posts: 7635
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Re:Communication between kernel and application

Post by Solar »

hunter wrote: But some times ago, the variant to put the pointer of the String into EBX and send it to the kernel, works fine ...
That was probably before you added seperate address spaces for user space.

The string exists at an address in user space. In order to access it there, your kernel must use the same virtual page -> physical page mapping as your application, or it will see something completely different at that address.
Every good solution is obvious once you've found it.
Post Reply