page fault processing

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.
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: page fault processing

Post by PeterX »

mrjbom wrote:

Code: Select all

pushad
push eax
popad
This combination of stack operations seems wrong to me.
User avatar
mrjbom
Member
Member
Posts: 322
Joined: Sun Jul 21, 2019 7:34 am

Re: page fault processing

Post 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.
Octocontrabass
Member
Member
Posts: 5885
Joined: Mon Mar 25, 2013 7:01 pm

Re: page fault processing

Post 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.
PeterX
Member
Member
Posts: 590
Joined: Fri Nov 22, 2019 5:46 am

Re: page fault processing

Post 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).
User avatar
mrjbom
Member
Member
Posts: 322
Joined: Sun Jul 21, 2019 7:34 am

Re: page fault processing

Post 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
Octocontrabass
Member
Member
Posts: 5885
Joined: Mon Mar 25, 2013 7:01 pm

Re: page fault processing

Post 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".
User avatar
mrjbom
Member
Member
Posts: 322
Joined: Sun Jul 21, 2019 7:34 am

Re: page fault processing

Post 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! ♥
Post Reply