OSDev.org

The Place to Start for Operating System Developers
It is currently Thu Mar 28, 2024 7:13 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 7 posts ] 
Author Message
 Post subject: MyOS example updated with `cld` instruction
PostPosted: Sun Jun 11, 2017 4:07 am 
Offline
Member
Member
User avatar

Joined: Wed Mar 21, 2012 3:01 pm
Posts: 930
Hi,

I maintain an example OS named MyOS that tries to have good implementations of basic things like the GDT, IDT, TSS, interrupt handling, PIC, and a general template for how to structure an operating system. It builds on top of my Meaty Skeleton tutorial from the wiki.

I've been linking this code a lot, especially the GDT ([1], [2]) and interrupt handling code ([3], [4]), as a way to get people to stop copying the old GDT and interrupt handling from from old tutorials, the same old bad implementations, and get people to copy better implementations instead. People have generally stopped criticizing this code when I link it, suggesting it's not obviously wrong, please let me know you find any problems.

Today I've updated the interrupt handler to add a cld instruction. This was a bad oversight. Without it, an user-space program could potentially have set the direction bit and kernel memory copies would have gone the wrong way, potentially being exploitable.

I'm posting a notification here in case you based your interrupt handling off my code, in which case you should make this change to your code as well.

If you didn't base your code off MyOS, assuming you are following the System V ABI normally used by toolchains such as gcc and clang, please confirm your code does not suffer from these problems:

  • You clear the direction flag with the cld instruction.
  • You set the kernel data segments (as user-space may be able to set them to the null segment, crashing your kernel) and restore the user-space segments afterwards.
  • You 16-byte align the stack in any assembly before using the call instruction. This is a hard System V ABI requirement for both 32-bit and 64-bit x86. This is not only for SSE, the compiler will assume this is the case and you risk having bad mysterious bugs down the road.
  • IRQ 2 cannot happen as it is only used internally for cascading IRQ 8 to IRQ 15. There is no reason to handle it and handling it only spreads the myth it can happen.
  • You don't have useless instruction sequences like
    Code:
    mov foo, %eax
    call *%eax
    which is easily simplified into
    Code:
    call foo
    .
  • Or any of James Molloy's Tutorial Known Bugs.

Thank you. If you find any further problems, please let me know so we can fight the community copying implementations full of old bugs.


Top
 Profile  
 
 Post subject: Re: MyOS example updated with `cld` instruction
PostPosted: Sun Jun 11, 2017 5:41 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
sortie wrote:
  • You don't have useless instruction sequences

This could be a ljmp instead of three instructions.


Top
 Profile  
 
 Post subject: Re: MyOS example updated with `cld` instruction
PostPosted: Sun Jun 11, 2017 7:14 pm 
Offline
Member
Member

Joined: Mon Jan 03, 2011 6:58 pm
Posts: 283
Your repository seems to be missing a license.

- Monk


Top
 Profile  
 
 Post subject: Re: MyOS example updated with `cld` instruction
PostPosted: Mon Jun 12, 2017 9:24 am 
Offline
Member
Member
User avatar

Joined: Wed Mar 21, 2012 3:01 pm
Posts: 930
Octocontrabass wrote:
This could be a ljmp instead of three instructions.


Thanks, I will think about this. I recall doing retf on purpose, or maybe just to show it off, as it is more dynamic than the odd ljmp syntax.

tjmonk15 wrote:
Your repository seems to be missing a license.

It is licensed under CC0 like the wiki and the meaty skeleton tutorial it is based on. I'll make a note to put the license in the repository for clarity.


Top
 Profile  
 
 Post subject: Re: MyOS example updated with `cld` instruction
PostPosted: Tue Jun 13, 2017 11:55 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5100
sortie wrote:
I recall doing retf on purpose, or maybe just to show it off, as it is more dynamic than the odd ljmp syntax.

There's no ljmp in long mode, so perhaps your x86_64 target would be a better place to show it off.


Top
 Profile  
 
 Post subject: Re: MyOS example updated with `cld` instruction
PostPosted: Thu Jun 15, 2017 6:28 am 
Offline
Member
Member

Joined: Sun Sep 06, 2015 5:40 am
Posts: 47
Hey; this is my first time seeing your code, and I think I have a lot more to learn, which is always good. In your interrupt handling stub, I noticed that you preserve CR2:
Code:
   movl %cr2, %ebp
   pushl %ebp

   ...

   popl %ebp
   movl %ebp, %cr2

My code doesn't and I haven't seen anywhere that recommends to do this, what is the rationale for doing this? I am not doubting that there's a good reason for it, I am pretty inexperienced.

_________________
OS on Github | My Rust ACPI library


Top
 Profile  
 
 Post subject: Re: MyOS example updated with `cld` instruction
PostPosted: Thu Jun 15, 2017 12:42 pm 
Offline
Member
Member
User avatar

Joined: Wed Mar 21, 2012 3:01 pm
Posts: 930
Hi BaconWraith,

There's no strict reason to preserve cr2. The register is set when a page fault happens. The kernel can inspect the register to find out what happened. If your page fault handler is always non-preemptive nor reentrant there's no reason to preserve it. However, if recursive page fault handling can happen, or if your kernel is preemptive during page fault handling, you may want to preserve it. In my OS, I consider it just another thread register. It avoids it being unintentionally trashed and lets my kernel threads be entirely pre-emptive even during user-space page faults (actually it's not preemptive in that case, since the page fault might have happened in the kernel, but the kernel thread is allowed to enable interrupts and become preemptive).


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

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot], DotBot [Bot] and 63 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