Skip to main content

Posts

Showing posts with the label What is Virtual Memory and How it works on Linux?

Privilege Levels in processor and Virtual Memory

Privilege Levels in processor and Virtual Memory Privilege Levels: The processor has Ring Levels 0-3, 0 being most privileged, 3 being least privileged. All processes in user land, be it root or non-root, execute in Ring 3. (Ring 1,2 are unused in Linux) A process in Privilege level 3 cannot read/write/execute segments which belong to supervisor (the kernel). So a write to memory locations higher than PAGE_OFFSET (3GB on i386) causes segmentation fault. Same goes for hardware access through in/out or mapped i/o (exceptions are iopl etc). Virtual Memory: Virtual Memory ensures no access to physical RAM pages by processes. Hence you *cannot* under any circumstances write a physical address, Hence there is ZERO chance of even reading kernel data, let alone messing them up.

What is Virtual Memory and How it works?

What is Virtual Memory and How it works? Virtual Memory allows multi-user as well as multi-programming computing environment that too for very less RAM. RAM use to be very costly earlier compare to present day. CPU has PC register from where next instruction can be fetched or data can be load/store. Assuming PC being 32 bit register, it can address 4G locations. But practically we put very less RAM, say 128MB only. Assuming Virtual Memory exists, compiler generates code for an application, where compiler is also aware of OS+Architecture. Now code generated by compiler assumes entire 4G location (so large extent) is available for it and it is the only application running on the CPU. So PC register shall always issue address which is one among 4G locations. So this 4G which is in fact physical capacity of PC register of CPU, is used as Virtual Address, as actual RAM is much less (0-128MB only) Now OS uses a table call Page Table along with MMU unit of CPU. Using this it loa