Darius
Darius

Reputation: 727

Virtual memory without hardware support

While reading this question and its answer I couldn't help but think why is it obligatory for the hardware to support virtual memory?

For example can't I simulate this behavior with software only (e.g the o.s with represent all the memory as some table, intercept all memory related actions and and do the mapping itself)?

Is there any OS that implements such techniques?

Upvotes: 4

Views: 869

Answers (1)

ikh
ikh

Reputation: 10417

As far as I know, No.

intercept all memory related actions? It doesn't look impossible, but I must be very very slow.

For example, suppose this code:

int f(int *a1, int *b1, int *c1, int *d1)
{
    const int n=100000;

    for(int j=0;j<n;j++){
        a1[j] += b1[j];
        c1[j] += d1[j];
    }
}

(From here >o<)

This simple loop is compiled into following by gcc -std=c99 -O3 with gcc 4.8.3:

push   %edi                     ; 1
xor    %eax,%eax
push   %esi                     ; 2
push   %ebx                     ; 3
mov    0x10(%esp),%ecx          ; 4
mov    0x14(%esp),%esi          ; 5
mov    0x18(%esp),%edx          ; 6
mov    0x1c(%esp),%ebx          ; 7
mov    (%esi,%eax,4),%edi       ; 8
add    %edi,(%ecx,%eax,4)       ; 9
mov    (%ebx,%eax,4),%edi       ; 10
add    %edi,(%edx,%eax,4)       ; 11
add    $0x1,%eax
cmp    $0x186a0,%eax
jne    15 <_f+0x15>             ; 12
pop    %ebx                     ; 13
pop    %esi                     ; 14
pop    %edi                     ; 15
ret                             ; 16

Even this really really simple function has 16 machine codes that access memory. Probably OS's simulate code has hundreds of machine codes, Then we can guess this memory-accessing codes' speed will slows down hundreds times at least.

Moreover, It's when you can watch only memory-accessing codes. Probably your processor doesn't have this feature, you should use step-by-step debugging, like x86's Trap Flag, and check every command every time.

Things goes worse - It's not enough to check codes. You may want IP (Instruction Pointer) to follow your OS's virtual memory rule as well, so you must check whether IP is over page's boundary after each codes was run. You also must very carefully deal with codes which can change IP, such as jmp, call, ret, ...

I don't think it can be implemented efficiently. It cannot be implemented efficiently. Speed is one of the most important part of operating system. If OS become a bit slow, all system is affected. In this case, it's not a bit - your computer gets slow lots and lots. Moreover, implementing this is very difficult as I say above - I'd rather write an emulator of a processor which has hardware-suppported virtual memory than do this crazy job!

Upvotes: 3

Related Questions