1

For example with following C code

#include <stdio.h>
int main() {
    unsigned long temp = 0x12345678;
    printf("temp address is %p\n", &temp);
    int* func_addr = (int*)main;
    printf("main address is %p\n", func_addr);
    int i;
    // suspend process
    scanf("%d", &i);
    return 0;
}

Compile the code on my machine ()and run the program in two terminals, and two process outputs:

Process 1:

temp address is 0xbfcc5350

main address is 0x80484bb

Process 2:

temp address is 0xbf94e5d0

main address is 0x80484bb

My questions are based on the figure Linear Address:

  1. main's virtual address are the same in two processes, and we know that virtual address equals to linear address, according to address translation from linear address and physical address, two identical virtual addresses should be mapped to two identical physical addresses, but actually two main's physical addresses are different, how is the mapping process?
  2. temp's addresses in two process are based on pages, their PGD part(high 10 bits) are the same (0x2ff), that means the two process has the same Page Table Entry?

My OS is Ubuntu 16.04.1 LTS, 32bit.

Joe.Wu
  • 23
  • 6
  • CR3 is different for each process. – ninjalj Sep 25 '16 at 16:47
  • See also: http://stackoverflow.com/questions/4022127/how-the-share-library-be-shared-by-different-processes – ninjalj Sep 25 '16 at 16:50
  • @ninjalj is right, each process has its PGD which is described on [kernel.org](https://www.kernel.org/doc/gorman/html/understand/understand006.html) – Joe.Wu Sep 27 '16 at 14:21

2 Answers2

0

In your example, two main() physical addresses may actually be same. Because the read-only code segment likely will be shared between the processes. But this does not mean that these processes share the page tables. The data sections are writable, so each process must have its own copy, mapped by it's own page table. Why both main's have same VA? Probably, to avoid patching the code with relocations, so it can be shared.

ddbug
  • 1,392
  • 1
  • 11
  • 25
  • Actually two main() physical addresses are different, their virtual addresses are the same, but was mapped to different physical addresses. – Joe.Wu Sep 27 '16 at 14:24
0

Each process has its own PGD, when it is running, CR3 register stores the physical address of the PGD. Further more, is PAE is enable, CR3 register stores the physical address of PDPT.

Joe.Wu
  • 23
  • 6