OSDev.org

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

All times are UTC - 6 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Only low 16 bits of address seem to matter in protected mode
PostPosted: Tue May 02, 2017 12:39 pm 
Offline

Joined: Tue Apr 04, 2017 2:37 pm
Posts: 19
When my C code in protected mode writes to address 0xa204, the value in the variable at 0x10a0e4 is also changed. It's as if only the low 16 bits are relevant. Is there something I'm overlooking about how protected mode and or the GDT works? I understand why this would happen in real mode; but, I don't see why it would happen in protected mode.

At the moment I have a very simple boot loader that follows the example of x86 bare metal (https://github.com/cirosantilli/x86-bare-metal-examples) and OSDev's Bare Bones to load the rest of the code, enter protected mode, and calls the C function that is the kernel entry point. I also have a few functions that can write using VGA text mode.

This is the "OS" code:

Code:
#include "icos.h"
#include "vga_text.h"


unsigned d1 = 0x57575757;  // initialized and put in .data section
unsigned not_initialized;  // uninitialized and put in .bss section

void bss_test() {

  not_initialized = 0x11223344;

  // Set up the VGA output
  vga_text_section_t head, body;
  vgat_initialize_head_body(&head, &body, 5);

  //print the addresses and values of the global data.
  vgat_write_unsigned_hex(&body, (unsigned)&d1, " <= &d1\n");
  vgat_write_unsigned_hex(&body, (unsigned)&not_initialized, " <= &ni\n");
  vgat_write_unsigned_hex(&body, not_initialized, " <= Original ni value\n");

  // Take the address of not_initialized, discard all but the low 16 bits,
  // then use that new value as a pointer.
  unsigned* pni = &not_initialized;
  unsigned* pni2 = (unsigned*) ((unsigned) pni &0xffff);
  vgat_write_unsigned_hex(&body, (unsigned) pni2, " <='fake' pointer\n");
  *pni2 = 0x55667788;

  // Modifying the "fake"/"truncated" pointer modifies not_initialized
  vgat_write_unsigned_hex(&body, not_initialized, " <= updated ni value\n");
 
}


Running this code produces the following output
Code:
0x9814 <= &d1
0x10a204 <= &ni
0x11223344 <= Original ni value
0xa204 <= 'fake' pointer
0x55667788 <= updated ni value


Top
 Profile  
 
 Post subject: Re: Only low 16 bits of address seem to matter in protected
PostPosted: Tue May 02, 2017 12:48 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
You will need to post your GDT code as well. The GDT has the ability to "shift" the starting value of the selector addresses.

_________________
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber


Top
 Profile  
 
 Post subject: Re: Only low 16 bits of address seem to matter in protected
PostPosted: Tue May 02, 2017 12:52 pm 
Offline
Member
Member
User avatar

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

kurmasz wrote:
Running this code produces the following output
Code:
0x9814 <= &d1
0x10a204 <= &ni
0x11223344 <= Original ni value
0xa204 <= 'fake' pointer
0x55667788 <= updated ni value


Looks like you forgot to enable A20...


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: Only low 16 bits of address seem to matter in protected
PostPosted: Tue May 02, 2017 1:00 pm 
Offline
Member
Member
User avatar

Joined: Fri Oct 21, 2011 9:47 pm
Posts: 286
Location: Tustin, CA USA
Brendan wrote:
Looks like you forgot to enable A20...

Missed that thought.

_________________
Adam

The name is fitting: Century Hobby OS -- At this rate, it's gonna take me that long!
Read about my mistakes and missteps with this iteration: Journal

"Sometimes things just don't make sense until you figure them out." -- Phil Stahlheber


Top
 Profile  
 
 Post subject: Re: Only low 16 bits of address seem to matter in protected
PostPosted: Tue May 02, 2017 2:18 pm 
Offline

Joined: Tue Apr 04, 2017 2:37 pm
Posts: 19
That was it: I hadn't set the A20 line.


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], SemrushBot [Bot] and 51 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