-1

I am writing a small hobby OS as a learning experience. It's aimed at a 32-bit x86 architecture.

I am at the point where I need to create an initial page_directory so I can enable paging. At this point paging (and thus VM) is not enabled.

I have a function that reserves 4kb of unused memory and returns the starting address of this memory block.

I want to create an array, page_dir (consisting of 1024 int), at the memory location returned by the function described above.

I understand the basic on pointers (I think), but I can't figure out how to do this.

How can I define the array page_table at the physical address returned during runtime?

chqrlie
  • 131,814
  • 10
  • 121
  • 189
ErwinM
  • 5,051
  • 2
  • 23
  • 35

2 Answers2

2

If I well understood you want treat an address returned by a function as the base address of an array of ints.
If the above assumption is correct you can use 2 ways, a cast or an intermediary variable.
Using a cast:

void *pd = GetPhysicalAddress();
...
for (i=0; i<1024; i++)
    ((int *)pd)[i] = SomeValue();    //cast for each access

Or:

int *pd = (int *)GetPhysicalAddress();    //Cast only on assignement
...
for (i=0; i<1024; i++)
   pd[i] = SomeValue();
Frankie_C
  • 4,764
  • 1
  • 13
  • 30
0

In general, you cannot do this for an actual physical address, but you can use mmap to get a pointer to memory at a specified virtual address. Mapping physical addresses such as device specific memory is usually done in device drivers using operating system specific APIs.

EDIT: With the extra information you provided, this is not the general case! In order to have a pointer to a physical address before the paging is even set up, I guess you can just use this:

p = (void*)0x00010000;

Or whatever actual physical address you want to use.

Even if paging is not set up, you may already be in protected mode with segmentation, so it really depends on how your DS segment is set up.

I suggest you study the bootstrap of actual operating systems, or just the bootloader that executes in the mode you are referring to.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • He is writing a hobby OS and he is setting up his own page directory this code is actually in the kernel (ring 0). Not sure how far long he is, and if he made his kernel POSIX compliant, so it is questionnable if discussing `mmap` is very useful? – Michael Petch Jan 03 '16 at 17:56
  • 1
    @MichaelPetch: I updated my answer. – chqrlie Jan 03 '16 at 18:06
  • Ah, I also didn't look at the questions edit history, so it appears things changed from his first version. – Michael Petch Jan 03 '16 at 18:09