Page 1 of 1

System calls problem [Closed]

Posted: Mon Dec 05, 2016 3:17 pm
by Ycep
Hi,
I have recently somehow broke my system calls.
The problem is that by some reason, parameters do not pass to requested function properly anymore.

Code: Select all

void Reserved()
{
	puts("This system call is reserved for future, and thus could not be used.\n");
}
void* sfunc[]=
{
	Reserved,Reserved,putc,puts,OpenFile,Reserved,ReadFile,Reserved,Reserved,Reserved,Reserved,Reserved,sleep,getch,Reserved,Reserved,Reserved
};
__declspec(naked) void SystemCallIrq()
{
	static uint16 no;
	_asm mov [no], bx
	if(no>16)_asm iretd
	static void* callsy=sfunc[no];
	_asm
	{
		push edi
		push esi
		push edx
		push ecx
		push ebx
		push eax
		call callsy
		add esp, 24
		iretd
	}
}
Basically, function number is being put in BX register and parameters are being put from register EAX-EDI.
Because this is an interrupt, to not corrupt any previously ran code I return stack by 6 entries.

Anywhooo?

Re: System calls problem

Posted: Mon Dec 05, 2016 4:40 pm
by Ch4ozz
Then open your kernel in a disassembler like IDA to check if the generated code is completely right.
You should code all parts using inline asm and dont switch so much to normal code because the compiler might **** up some stuff.
Also what does "not properly" mean? Does it crash or wrong values?

Re: System calls problem

Posted: Tue Dec 06, 2016 4:35 am
by MollenOS
You should really stop mixing inline assembly with C code in crucial functions like that. Also in a system call it would be beneficial not to push/pop eax so you can support return codes. Write that system call in pure assembly, and don't use static variables in a function that might be called from multiple threads

Re: System calls problem

Posted: Tue Dec 06, 2016 9:42 am
by issamabd
Your asm_ instruction doesn't have any "side effects"! The compiler can move it from its place during optimization. It can also delete it! So create some dependencies between it and the C code and use the "volatile" keyword.

How many level of protection your OS uses ? What does the callsy function ?

If you are using more than one level of protection/privilege, during the system call execution the DS,ES,FS,GS registers still point to the old address space!

Re: System calls problem [Closed]

Posted: Tue Dec 06, 2016 1:40 pm
by Ycep
Hell ye'eh! I fixed it myself.

Re: System calls problem [Closed]

Posted: Tue Dec 06, 2016 3:33 pm
by Boris
Can you describe how ? Nothing more frustrating for people to find your post while trying to resolve a similar problem and find no answers.

Thanks.