4

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?

Community
  • 1
  • 1
Darius
  • 707
  • 6
  • 21
  • for most processors, the mmu is what tells the os about memory accesses. if it doesn't support virtual memory, you will probably not be able to implement it in software without recompiling all the code with a special compiler. it would also be very slow. (like 2 - 10x slower) – programmerjake Sep 16 '14 at 11:26
  • Like @programmerjake says, it would be horrendously slow. In effect, your 'software memory manager' would turn the processor into an interpreter. MMU hardware allows the processor to run executable code natively until a page-fault interrupt is generated by the hardware and handled. – Martin James Sep 16 '14 at 11:31
  • @programmerjake I'm trying to understand whether it's possible, I'm well aware that it might slow things down. Why do you think that I'd need to recompile everything ? For example I could write something like an emulator that will translate the binary in real time, can't I ? (Again, I'm not concerned about speed) – Darius Sep 16 '14 at 11:31
  • You could do that, I suppose. AFAIK, nobody has ever done it because it's like driving a nail with a feather when a hammer is available. – Martin James Sep 16 '14 at 11:34
  • 1
    @Darius yes, you could emulate the processor. i was referring to converting all memory references to do a simulated page table lookup in the compiler, bscause that's not emulating the entire processor, just the mmu. – programmerjake Sep 16 '14 at 11:35
  • It's not that easy. Indirection etc. means that the addresses of memory accesses are not known until runtime. If they were known, there would never be any segfaults/Access Violations :) – Martin James Sep 16 '14 at 11:42
  • @MartinJames Oh, did not hope to find a _fast_ or _simple_ solution, just a viable and implementable one :) – Darius Sep 16 '14 at 11:46

1 Answers1

3

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!

Community
  • 1
  • 1
ikh
  • 10,119
  • 1
  • 31
  • 70
  • Yeah - and don't offer me the job either:) – Martin James Sep 16 '14 at 11:48
  • The phrasing is a little confusing, it seems that both doing this "crazy job" (as you call it :) ) and emulating the whole processor will accomplish the discussed goal. So why the bold **No** ? Seems like it should be "Yes but it'll be slow as hell." (or slow as an emulator which is not entirely different) – Darius Sep 16 '14 at 11:58
  • @Darius read my last paragraph again (or you've read un-edited version >o<) Speed will get terrible, and OS shouldn't do. – ikh Sep 16 '14 at 12:00
  • @Darius If you manage to do it anyway, we'd rather call it "processor emulator" or "virtual machine", than "operating system". – ikh Sep 16 '14 at 12:02
  • @ikh Still, I don't think that slow = impossible :) I just think that saying no and then explaining how it _can_ be done is rather confusing. – Darius Sep 16 '14 at 12:07
  • @Darius so, your question is just about whether it's possible, with no regard of speed? Yes it's possible *theorically*, although these points. – ikh Sep 16 '14 at 12:11
  • @ikh Yes, I've never mentioned any performance requirements, plus I've mentioned this in the comments: [1](http://stackoverflow.com/questions/25867304/virtual-memory-without-hardware-support/25867750?noredirect=1#comment40478121_25867304) [2](http://stackoverflow.com/questions/25867304/virtual-memory-without-hardware-support/25867750?noredirect=1#comment40478688_25867304) :) – Darius Sep 16 '14 at 12:17