How to protect memory in contiguous memory allocation methods ?

In order to understand the concept of Memory protection first we need to know that in Memory each user process has its own address space. So Operating system must be protected from user process and each user address space should be protected from other user process.

First there is a need to determine the legal address that a process may access and to ensure that process may access only these legal addresses. Protection can be provide by using two register known as Base and Limit register. The base register holds the smallest legal physical memory address; the limit register specifies the size of the range. For example, if the base register holds 300040 and the limit register is 120900, then the program can legally access all addresses from 300040 through 420939 (inclusive).

Protection of memory space is accomplished by having the CPU hardware compare every address generated by the CPU with the values of limit register. Any attempt by a program executing in user mode to access operating-system memory or other users' memory results in a trap to the operating system, which treats the attempt as a fatal error . This scheme prevents a user program from (accidentally or deliberately) modifying the code or data structures of either the operating system or other users.

base and limit registers

                                      Figure 1: Memory Protection in Contiguous Allocation Methods

The base and limit registers can be loaded only by the operating system, which uses a special privileged instruction. Since privileged instructions can be executed only in kernel mode, and since only the operating system executes in kernel mode, only the operating system can load the base and limit registers. This scheme allows the operating system to change the value of the registers but prevents user programs from changing the registers' contents. 

The operating system, executing in kernel mode, is given unrestricted access to both operating system memory and users' memory. This provision allows the operating system to load users' programs into users' memory, to dump out those programs in case of errors, to access and modify parameters of system calls, and so on.

Each logical address should be less than the limit register value.

Memory management Unit convert each convert each logical address to physical address by adding the value of relocation register.

When the CPU scheduler selects a process for execution, the dispatcher loads the relocation and limit registers with the correct values as part of the context switch. Because every address generated by a CPU is checked against these registers, we can protect both the operating system and the other users' programs and data from being modified by this running process. 

Keywords: definition of base register, definition of relocation register, definition of memory management unit, how to protect memory in contiguous allocation, logical address and physical address in memory management, how to convert logical address to physical address?