Page 1 of 2
Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 6:39 am
by 0x57
If I make a Segmented memory in Real Mode, but later change it to a Paged one in Long Mode, I have couple of options like switching to the compatibility mode to run the old applications, right? I'm wondering is there any chance to make the memory management mechanism totally transparent to the applications, so they could run without modifications/recompile and without changing the processor mode, which means your Real Mode application would run in Protected or Long Mode and vice versa.
Sounds Stupid-Impossible question, however I appreciate any ideas or inspirations, thanks! :)
P.S. I was thinking of adding a proxy between the application and memory manager. It could provides an API for the program so you can add kinda of meta-data to the executable files' header section, so the proxy would do the proper modifications to the binary, and then try to execute it.
P.P.S. I know that we do the similar thing in Relocation Table, however I think this one needs more modifications on the binary ...
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:20 am
by iansjack
Well, I'd say that memory management should always be a black box as far as applications are concerned. You call malloc() when you want memory, free() when you need it no longer (or you use some sort of garbage collection), and care not a fig how that is implemented. But a recompile will almost always be needed if you change the underlying memory model. I see no problem with that.
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:29 am
by qw
0x57 wrote:I'm wondering is there any chance to make the memory management mechanism totally transparent to the applications, so they could run without modifications/recompile and without changing the processor mode, which means your Real Mode application would run in Protected or Long Mode and vice versa.
This is not just a matter of memory management. Machine code may have different meanings in different modes. For example, B8 34 12 BB 78 56 means in real mode, protected mode and long mode respectively:
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:31 am
by 0x57
iansjack wrote:But a recompile will almost always be needed if you change the underlying memory model. I see no problem with that.
Thanks iansjack, but two questions:
First, in which cases you mean a recompile will be needed? Could you give me a clear example, if you have any in mind.
Second, I thought that none of the major operating systems employing that, because (for example) you need an emulator to run a 16-bit program on 32-bit system. Do you know anywhere which there would be a documented stuff about using this technique in any operating system?
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:35 am
by 0x57
Thanks Hobbes ... well, something like a virtual machine and byte-code technique (e.g. java) would works then, right? just slower ...
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:37 am
by bluemoon
You can subdivide "memory allocation" into different implementation layers:
KERNEL SIDE
1. page allocator (aka physical memory manager)
2. logical memory manager (this handle page mapping, etc)
3. sbrk() or HeapAlloc() alike API that enable application to request memory.
APPLICATION SIDE
4. malloc()/free() that manage memory pool allocated with sbrk/HeapAlloc; you may have multiple version of malloc.
Usually you don't need to rebuild application if you altered implementation on kernel side and keep the interface unchanged.
However, changing from 16-bit segmented to 64-bit basically require different pointer representation, ie. interface changes; and recompile is almost unavoidable if you want to actually make use of the extra bits.
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:38 am
by 0x57
0x57 wrote:First, in which cases you mean a recompile will be needed? ...
By the way, I guess Hobbes spot that ...
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:42 am
by bluemoon
0x57 wrote:which means your Real Mode application would run in Protected or Long Mode and vice versa.
No, If you enabled long mode (setting MSR, either 64-bit long mode or 32-bit compatibility mode), you no longer able to natively run 16-bit code.
You can however implementa an emulator for that.
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:42 am
by 0x57
bluemoon wrote:... recompile is almost unavoidable if you want to actually make use of the extra bits.
Thanks bluemoon. You're right, but I'm wondering how it could be possible to avoid that somehow, by just doing some "reasonable" stuff at/before run-time ...
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:45 am
by bluemoon
You can avoid recompile of 16-bit application if you emulate the 16 bit environment - write your own or port DOSBOX; and provide/wrap/expose related APIs to it.
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:46 am
by 0x57
bluemoon wrote:0x57 wrote:which means your Real Mode application would run in Protected or Long Mode and vice versa.
No, If you enabled long mode (setting MSR, either 64-bit long mode or 32-bit compatibility mode), you no longer able to natively run 16-bit code.
You can however implementa an emulator for that.
Well, I meant that you modify the binary before the actual execution to avoid that, so it would not use/need legacy stuff anymore and will looks like a normal guy (from the processor and OS prospective).
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:48 am
by bluemoon
0x57 wrote:Well, I meant that you modify the binary before the actual execution to avoid that
Possible in theory, but binary translation is an very difficult topic of its own - its the core art of most emulator anyway.
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:54 am
by Brendan
Hi,
0x57 wrote:If I make a Segmented memory in Real Mode, but later change it to a Paged one in Long Mode, I have couple of options like switching to the compatibility mode to run the old applications, right?
In long mode, you can execute code using the "16-bit compatibility submode"; and in protected mode you can use 16-bit code segments. This might allow you to run the same executable code (without recompiling) under both real mode and the "16-bit compatibility submode" of long mode and protected mode; as long as the code doesn't make any assumptions about the contents of segment registers (which also means that things like "malloc()" would have to be implemented in the kernel or in a shared library and can't be part of the application).
For example, typically real mode code will change segment registers (e.g. "
offset++; if(offset == 0) { offset -= 16; segment++; }") to work around "64 KiB segment" limitations; and you'd have to make sure your 16-bit executables don't do that (in case the segment is actually a descriptor in the GDT). Of course this means that working with anything larger than 64 KiB (e.g. a decent size text file, a small array, a tiny photo, etc) will be a complete nightmare. Ironically, 16-bit code running in protected mode or long mode can use 32-bit data segments (where you wouldn't need to work around "64 KiB segment" limits if you didn't need real mode compatibility).
Mostly, in theory it's entirely possible. However, in practice you will never convince any sane programmer to write a 16-bit application and will also never convince any sane end user to use a real mode OS, so it's a complete waste of time to begin with.
Cheers,
Brendan
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 7:56 am
by 0x57
bluemoon wrote: ... its the core art of most emulator anyway.
I never thought about that! I would take a look at some resources to see how they're doing that like fail-safe process. I don't want to do some crap on every application right before launch it!
By the way, do you think a virtual machine approach would be a reasonable solution?
Re: Could Memory-Managment seem transparent to the programs?
Posted: Tue Jan 08, 2013 8:02 am
by 0x57
Brendan wrote:Mostly, in theory it's entirely possible. However, in practice you will never convince any sane programmer to write a 16-bit application and will also never convince any sane end user to use a real mode OS, so it's a complete waste of time to begin with.
Thanks Brendan, well just a little bit brainstorming here. That's always nice to measure your ideas publicly!
P.S. Theory and Practice should be the same, by the way ... however I know that they are not, anyways!