OSDev.org

The Place to Start for Operating System Developers
It is currently Tue Apr 23, 2024 1:56 am

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Modifying Control Registers x86
PostPosted: Mon Feb 07, 2022 7:04 pm 
Offline

Joined: Mon Feb 07, 2022 6:54 pm
Posts: 1
I am studying the xv6 Operating System (written for 32-bit x86 architectures) as part of a course at university and have noticed a pattern while modifying the control registers (specifically, CR0 and CR4). The pattern is the following three assembly instructions (CR4_PSE is the Page Size Extension bit, though not relevant to my question):

Code:
movl %cr4, %eax
orl    $(CR4_PSE), %eax
movl %eax, %cr4


My question is - why can't we modify the CR4 register directly? I have anecdotally heard my professor say the answer is that it is more costly to modify certain CPU registers like the CR4 or CR0 registers, but I haven't been able to find evidence backing this (in the form of specifications). I would be grateful to learn more about why this cost difference is there and where to read about it.


Top
 Profile  
 
 Post subject: Re: Modifying Control Registers x86
PostPosted: Mon Feb 07, 2022 10:47 pm 
Offline
Member
Member

Joined: Wed Mar 30, 2011 12:31 am
Posts: 676
Each instruction needs to encode what operands it will use, and if control registers were included in the set of possible registers for other instructions than MOV that would require a lot of extra bits. In fact, the instruction for moving to and from control registers is really an entirely different instruction that just gets assigned the same mnemonic, and instructions for other operations involving control registers simply don't exist.

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


Top
 Profile  
 
 Post subject: Re: Modifying Control Registers x86
PostPosted: Thu Feb 10, 2022 11:17 pm 
Offline
Member
Member

Joined: Sun Jun 23, 2019 5:36 pm
Posts: 618
Location: North Dakota, United States
Yep, you can't -- say -- use an OR or XOR instruction on a control register directly. If I'm not mistaken you'll get an invalid opcode exception if you try doing that. The read-modify-write pattern is used because that's the *only* way to do it.


Top
 Profile  
 
 Post subject: Re: Modifying Control Registers x86
PostPosted: Fri Feb 11, 2022 12:32 am 
Online
Member
Member
User avatar

Joined: Sat Mar 31, 2012 3:07 am
Posts: 4594
Location: Chichester, UK
They are not instructions that are used very frequently compared to others. Hence there is no need to make them more efficient.


Top
 Profile  
 
 Post subject: Re: Modifying Control Registers x86
PostPosted: Fri Feb 11, 2022 11:07 am 
Offline
Member
Member

Joined: Mon Mar 25, 2013 7:01 pm
Posts: 5143
Ethin wrote:
If I'm not mistaken you'll get an invalid opcode exception if you try doing that.

You'll get an assembler error, because there's no way to encode that combination of instruction and operands.

You might be thinking of segment instructions with CS as the destination register. You can encode those, and they will cause an invalid opcode exception.


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

All times are UTC - 6 hours


Who is online

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