Page 2 of 2

Re: page fault processing

Posted: Fri Jul 03, 2020 3:51 pm
by PeterX
mrjbom wrote:

Code: Select all

pushad
push eax
popad
This combination of stack operations seems wrong to me.

Re: page fault processing

Posted: Fri Jul 03, 2020 3:54 pm
by mrjbom
PeterX wrote:
mrjbom wrote:

Code: Select all

pushad
push eax
popad
This combination of stack operations seems wrong to me.
I don't quite understand it either.
Most likely, I misunderstood what @nexos meant.

Re: page fault processing

Posted: Fri Jul 03, 2020 4:11 pm
by Octocontrabass
mrjbom wrote:

Code: Select all

  pushad
  ;save error code from stack to eax
  mov eax, [esp + 4]
But the error code is not at ESP+4 here.
mrjbom wrote:But my С function doesn't have any parameters...
Your C function must have parameters if you want to pass values to it. The compiler will clobber EAX because it doesn't know you're trying to pass a value that way.

The System V ABI requires you to pass parameters on the stack, but GCC supports other calling conventions, including ones that pass some parameters in registers.

You should remove CLI/STI. If you want interrupts disabled, change the IDT to use an interrupt gate.

Re: page fault processing

Posted: Fri Jul 03, 2020 4:12 pm
by PeterX
You need a counterpart for every stackoperation.

Code: Select all

For example:
pushad
push eax
... (place the call here)
pop eax (or "ADD esp, 4" will do the work, too)
popad
And what nexos is trying to tell you is a so-called "stackframe". It is used for C function calls (and every function using C functions, be it assembler, Pascal or what else).

Re: page fault processing

Posted: Fri Jul 03, 2020 4:40 pm
by mrjbom
PeterX wrote:You need a counterpart for every stackoperation.

Code: Select all

For example:
pushad
push eax
... (place the call here)
pop eax (or "ADD esp, 4" will do the work, too)
popad
And what nexos is trying to tell you is a so-called "stackframe". It is used for C function calls (and every function using C functions, be it assembler, Pascal or what else).
OK, now I pass error code through the function parameters. Here is my code, as advised.
The parameter is passed successfully and everything seems fine...

But the problem is that the function loops, as if an exception is constantly triggered, and the processor is not reset, although I clear the stack of the error code.

Code: Select all

page_fault:
  cli
  ;save all 32bit registers
  pushad
  ;pass error code in function argument
  push eax
  call page_fault_handler
  pop eax
  ;return all 32bit registers
  popad
  ;delete error code from stack
  add esp, 4
  sti
  iretd

Re: page fault processing

Posted: Fri Jul 03, 2020 4:43 pm
by Octocontrabass
Do you update the page tables so the faulting instruction will not cause another page fault? If you make no changes to the page tables, it will just fault again and again.

You are still not passing the error code to your C function. Try "push [esp+32]" instead of "push eax".

Re: page fault processing

Posted: Fri Jul 03, 2020 5:04 pm
by mrjbom
Octocontrabass wrote:Do you update the page tables so the faulting instruction will not cause another page fault? If you make no changes to the page tables, it will just fault again and again.

You are still not passing the error code to your C function. Try "push [esp+32]" instead of "push eax".
Now I pass the error code as you said.
Everything really works as it should.

Code: Select all

page_fault:
  ;save all 32bit registers
  pushad
  ;pass error code in function argument
  push dword [esp + 32]
  call page_fault_handler
  pop dword [esp + 32]
  ;return all 32bit registers
  popad
  ;delete error code from stack
  add esp, 4
  iretd
Thank you all for your help! ♥