Page 1 of 1
Communication between kernel and application
Posted: Thu Jul 06, 2006 4:03 am
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 ...
Re:Communication between kernel and application
Posted: Thu Jul 06, 2006 4:50 am
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?
Re:Communication between kernel and application
Posted: Thu Jul 06, 2006 4:54 am
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...
Re:Communication between kernel and application
Posted: Thu Jul 06, 2006 5:11 am
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 ...
Re:Communication between kernel and application
Posted: Thu Jul 06, 2006 5:44 am
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

Re:Communication between kernel and application
Posted: Thu Jul 06, 2006 7:30 am
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?
Re:Communication between kernel and application
Posted: Thu Jul 06, 2006 7:43 am
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.
Re:Communication between kernel and application
Posted: Thu Jul 06, 2006 8:09 am
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 ...
Re:Communication between kernel and application
Posted: Thu Jul 06, 2006 8:33 am
by hunter
Sorry I have forgotten to say that I use Paging ...
Re:Communication between kernel and application
Posted: Thu Jul 06, 2006 8:50 am
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.