OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 11:19 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Page fault handler causes read data to be corrupt?
PostPosted: Thu Dec 28, 2017 10:12 am 
Offline

Joined: Sat Jul 20, 2013 10:06 pm
Posts: 9
It seems after I map a page in my page fault handler, the data at the paged address is corrupt on the first read, but fine on the second.

Here is my page fault handler:

Code:
.globl  exception_page_fault
.align  4

exception_page_fault:
    cld
    push %esp
    call pg_fault   
    add $0x08,%esp # 4 bytes for error code, 4 bytes for saved ESP
    iret


The signature for pg_fault in C:

Code:
void pg_fault(uint32_t esp, uint32_t ec); // esp = ESP upon entry to fault handler; ec = error code


Currently, when pg_fault is called, it identity maps the page that isn't present. It's a temporary solution so I can read ACPI tables while paging.

I have a test that shows that shows the Length field in the RSDT table:

Code:
printf("first try: %u\n", acpi_rsdt->length);
printf("second try: %u\n", acpi_rsdt->length);


The first try triggers a page fault, which identity maps the page. The first read attempt returns something resembling a real mode address in the ROM (like a vector in the IVT). The second try returns the correct value. I would expect both of these to be the same. It even reads fine in the page handler right after reloading CR3 and calling INVLPG on the address. I'm pretty sure there is something wrong with my assembly page fault handler, but I can't tell what it is. I'm using Virtualbox 4.3.10.


Top
 Profile  
 
 Post subject: Re: Page fault handler causes read data to be corrupt?
PostPosted: Thu Dec 28, 2017 10:34 am 
Offline
Member
Member

Joined: Thu May 17, 2007 1:27 pm
Posts: 999
The ABI does not guarantee that all registers are saved by pg_fault(). You need to look up which registers are caller-saved in your ABI and manually save and restore them.

_________________
managarm: Microkernel-based OS capable of running a Wayland desktop (Discord: https://discord.gg/7WB6Ur3). My OS-dev projects: [mlibc: Portable C library for managarm, qword, Linux, Sigma, ...] [LAI: AML interpreter] [xbstrap: Build system for OS distributions].


Top
 Profile  
 
 Post subject: Re: Page fault handler causes read data to be corrupt?
PostPosted: Thu Dec 28, 2017 10:35 am 
Offline
Member
Member
User avatar

Joined: Thu Aug 11, 2005 11:00 pm
Posts: 1110
Location: Tartu, Estonia
I would assume that your interrupt handler corrupts the interrupted code's registers. Where do you save / restore them?

_________________
Programmers' Hardware Database // GitHub user: xenos1984; OS project: NOS


Top
 Profile  
 
 Post subject: Re: Page fault handler causes read data to be corrupt?
PostPosted: Thu Dec 28, 2017 10:58 am 
Offline

Joined: Sat Jul 20, 2013 10:06 pm
Posts: 9
I did not consider saving registers. I am using the sysV ABI (i686-elf), which saves EBX, ESI, EDI, EBP, and ESP. I found that if I do a PUSHA at the beginning and a POPA before the IRET and stack adjustment (for error codes), it works fine. Thanks


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 70 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group