OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 7:41 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 12 posts ] 
Author Message
 Post subject: control weather page can be executed on i386
PostPosted: Sun Jul 22, 2018 2:30 pm 
Offline

Joined: Mon Jul 16, 2018 10:23 pm
Posts: 6
So when loading an executable you want to prevent anything more from being executable than is possible. You can control this with segments for instance. It would seem that on i386 you cannot control weather a page is executable or not, however. Is segmentation the only way to control whether or not a page can be executed on i386?


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Sun Jul 22, 2018 2:49 pm 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
Paging lets you control this. Most modern operating systems don't use segmentation.


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Sun Jul 22, 2018 3:01 pm 
Offline

Joined: Mon Jul 16, 2018 10:23 pm
Posts: 6
What bit do I set in the page table to allow this? On https://wiki.osdev.org/Paging there doesn't seem to be any mention of a way to control if code is executable or not.


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Sun Jul 22, 2018 3:04 pm 
Offline
Member
Member

Joined: Sat Feb 27, 2010 8:55 pm
Posts: 147
iansjack wrote:
Paging lets you control this. Most modern operating systems don't use segmentation.


He was asking about the 386; the NX bit wasn't added until much later (Pentium 2?)

So yes, on old hardware segmentation is the most straightforward way to prevent data from being executed. There is an interesting hack to emulate NX on systems that don't support it; it has something to do with tricking TLB entries. I can't quite remember how it worked.


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Sun Jul 22, 2018 3:15 pm 
Offline

Joined: Mon Jul 16, 2018 10:23 pm
Posts: 6
That makes it sound like there's still a way to do this with a 32-bit kernel. I'm using qemu so I'd just like to be able to load a 32-bit x86 kernel and be able to set the executable bit.


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Sun Jul 22, 2018 4:17 pm 
Offline
Member
Member

Joined: Sun Sep 06, 2015 5:40 am
Posts: 47
Quote:
He was asking about the 386; the NX bit wasn't added until much later (Pentium 2?)


Afaik, Intel first supported it on the Pentium 4, so even later. AMD supported it before that however, I think from the start of AMD64

_________________
OS on Github | My Rust ACPI library


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Sun Jul 22, 2018 7:18 pm 
Offline

Joined: Mon Jul 16, 2018 10:23 pm
Posts: 6
Ah so if you enable PAE you get access to the NX bit. I see.


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Mon Jul 23, 2018 4:30 am 
Offline
Member
Member
User avatar

Joined: Sat Jan 15, 2005 12:00 am
Posts: 8561
Location: At his keyboard!
Hi,

lambdaislove wrote:
Ah so if you enable PAE you get access to the NX bit. I see.


Yes; if you enable PAE you may or may not get access to the NX bit depending on whether the CPU supports that feature or not.

azblue wrote:
There is an interesting hack to emulate NX on systems that don't support it; it has something to do with tricking TLB entries. I can't quite remember how it worked.


For most CPUs (especially those that don't support NX), "instruction TLB" and "data TLB" are separate. This means that you can mark all "non-executable" pages as "not present" and get a page fault when they're accessed; and if it was a data access your page fault handler can mark the page as "present" and read from the page (to get it into the data TLB but not the instruction TLB) and then make the page "not present" again. Of course once it's in the data TLB it'll stay there for a while (until evicted or flushed) and other data accesses to that page won't cause a page fault (even though the page is "not present"), but execution will cause a page fault (because it's only in the data TLB and not in the instruction TLB).


Cheers,

Brendan

_________________
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Mon Jul 23, 2018 9:16 am 
Offline
Member
Member
User avatar

Joined: Fri Oct 27, 2006 9:42 am
Posts: 1925
Location: Athens, GA, USA
It should, however, be mentioned that AMD introduced the AMD64 architectural extensions, including the NX bit, in 2003, and Intel followed suit with their XD bit (which is their name for the same setting) a year later. While systems without it continued to be sold for a few more years, by now one can reasonably expect that any PC made in the past (say) 13 years to support NX/XD bits.

Why is this relevant? Because that was still in the period (which stretched from 1974 to around 2008) where each successive generation of hardware outsold all previous generations combined - that is to say, roughly speaking, the number of microprocessor-based computers in the world more than doubled every 18 to 24 months. Technically, this trend has continued, but most of that rapid increase is now seen in the mobile market rather than desktops, where x86 isn't a serious competitor and every processor in use has had an equivalent functionality from the outset (at least WRT the ones used in mobile devices).

The practical upshot of this is that between pattern, and older systems discarded for hardware failures or obsolescence, the number of PCs without the NX/XD bits currently in use is negligible, at least from the perspective of most OS devs. The majority of systems which don't have it are ones which aren't going to get a new OS put on them no matter what, not even a version of Windows made in the past decade - old imaging file servers which have been hidden in an electrical closet of some dentist's office slowly dying of overheating and dust, glorified kiosks running on the reception desk of some government agency, that sort of thing. There are hundreds of thousands of such systems, certainly, but no one is going to be changing the OS on them, ever - with such systems, you'd be lucky to find Windows XP or Red Hat 9.0 on them.

Where I am currently still exiled to... sorry I meant to say :cry: , where I currently live in the southern US, even IT recycling businesses don't sell computers that are older than Pentium 4, and they basically charge a nominal fee (US$20-35) to let you take them off their hands, which they only do to defray some of their costs (it is a lot cheaper than stripping down the metals and chemicals, and then dealing with the resulting toxic waste).

I cannot speak for places outside the US, but my impression is that in most of the world, you are even less likely to find older hardware. I may be wrong, however; please feel free to correct me on this.

The point is, you probably would need to go out of your way to find a system without NX/XD support.

Now, I do not know what the OP's goals are, or what hardware they mean to target. There are legitimate reasons why they might want to target base i386 systems. The question also speaks to the issues of supporting the necessary subsystems (PAE or Long Mode) in order to use the NX/XD bit, which may be a consideration for the OP when starting out in an new operating system implementation. However, I do think that some perspective may be helpful, if not for the OP then for some others with similar questions.

_________________
Rev. First Speaker Schol-R-LEA;2 LCF ELF JAM POEE KoR KCO PPWMTF
Ordo OS Project
Lisp programmers tend to seem very odd to outsiders, just like anyone else who has had a religious experience they can't quite explain to others.


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Mon Jul 23, 2018 10:11 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
Perhaps the OP could confirm that the question referred only to the original 80386 rather than current processors?


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Mon Jul 23, 2018 2:30 pm 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
iansjack wrote:
Perhaps the OP could confirm that the question referred only to the original 80386 rather than current processors?

OP already confirmed that they weren't talking about the original 80386:
lambdaislove wrote:
That makes it sound like there's still a way to do this with a 32-bit kernel. I'm using qemu so I'd just like to be able to load a 32-bit x86 kernel and be able to set the executable bit.

_________________
toaruos on github | toaruos.org | gitlab | twitter | bim - a text editor


Top
 Profile  
 
 Post subject: Re: control weather page can be executed on i386
PostPosted: Tue Jul 24, 2018 11:05 am 
Offline
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
I think you'll find that processors later than the 80386 can run 32-bit kernels. Some of them even support PAE.


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

All times are UTC - 6 hours


Who is online

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