-3

After allocating a logical memory space for a particular process, protection to that space is enforced by using a base register and limit register. To defend against accessing memory outside of the allocated memory space for a process, CPU hardware compares each requested memory address to these registers. If one of the two following conditions holds, an operating system trap occurs:

  1. The target address is less than the base register.
  2. The target address is greater than or equal to the limit register.

This logic can be visually observed in Figure 7.2 below.

Figure 7.2

The above explains how they occur, e.g.

any attempt of a program executing in user mode to access operating-system memory or other user’s memory results in a trap to the operating system.

But what are some examples of when the requested address would actually be outside of the program's assigned memory space? What would cause this?

[Image and quote source: Operating System Essentials, 2nd Ed. - Abraham Silberschatz]

8protons
  • 3,591
  • 5
  • 32
  • 67
  • DMA is not mentioned in your question or text. – Basile Starynkevitch May 11 '18 at 05:39
  • Your question is really unclear. – Basile Starynkevitch May 11 '18 at 05:45
  • Yes, the wikipage on DMA explains similar things. It is relevant only *inside* the kernel, not in user space. – Basile Starynkevitch May 11 '18 at 05:59
  • I maintain that your question is unclear. It does not mention user-space vs kernel-space, which is central for DMA. The picture makes sense only for user-space (kernel code cannot, by definition, trap to the OS, since it is *implementing* the OS abstractions). DMA happens only in kernel mode, and user-mode does not see it. The kernel cannot trap to operating system monitor, because it *is* that operating system monitor – Basile Starynkevitch May 11 '18 at 06:01
  • Again, trapping to OS monitor (another name for OS kernel) does not make any sense inside the kernel itself. It makes sense only in user mode – Basile Starynkevitch May 11 '18 at 06:05
  • You should explicit your question (and surely your book mention kernel mode vs user mode). The picture cannot have any sense in kernel mode (you don't have an infinite tower of kernels). It can have sense only for user mode. And DMA happens only in kernel mode. – Basile Starynkevitch May 11 '18 at 06:06
  • Then give a link in your question. I don't know that precise book (I mention http://pages.cs.wisc.edu/~remzi/OSTEP/ in my answer) and I did not read it, and I believe you are misunderstanding something (since the figure cannot have any sense in kernel mode) – Basile Starynkevitch May 11 '18 at 06:08
  • I answered to your question: examples of faulty requested addresses happen in user mode, when some invalid address (like `NULL`) is dereferenced. – Basile Starynkevitch May 11 '18 at 06:14

1 Answers1

2

In practice, current hardware (e.g. x86-64 PC, ARM tablets, ...) don't use a base & limit registers (but the i286 did) anymore but has an MMU.

Read about MMU, paging, segmentation fault, virtual memory, virtual address space, and Operating System: Three Easy Pieces (freely downloadable)

But what are some examples of when the requested address would actually be outside of the program's assigned memory space?

Any kind of segmentation fault in user space, in particular dereferencing the NULL pointer (or some "uninitialized" pointer variable which contains a random bits pattern outside of the address space). Such bugs are common in C programs (in user space). For a simplified example:

int *ptr = NULL;
/// some long code or execution which does not change the `ptr` value
*ptr = 34; // SEGMENTATION FAULT 

The kernel code should be trusted, and does not get any segmentation fault (by hypothesis). Kernel code misbehaving and dereferencing an invalid address would crash the computer.

(in practice, big kernels like Linux have some code which might "partly" handle segmentation faults in some parts of the kernel, e.g. some drivers; but the general idea is that kernel code should be trusted and bug-free).

BTW, your question don't mention DMA (but your comments do). In practice, DMA is managed only by the kernel (e.g. for physical disk I/O) which ensure, before configuring and starting it, that the entire block addresses are valid (and paged in). So in practice wrong addresses in DMA never happens (depending on the hardware DMA could use physical or virtual addresses). More generally, the kernel is expected to avoid bugs like segmentation fault or other kind of bad addresses: user space can have bad addresses (the kernel makes segmentation faults when that happens, and on Unix gives some SIGSEGV signal to the application), but kernel space should be trusted code using only good addresses.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • On some hardware DMA uses only physical memory and don't even know about base & limit registers. On every computer DMA is a very low level operation that only the kernel should know and handle. Application programs and user mode don't see DMA – Basile Starynkevitch May 11 '18 at 05:52
  • Wrong. It is not about main memory, but about virtual memory. You feel still confused. – Basile Starynkevitch May 11 '18 at 06:26
  • So basically code has to be written that explicitly tries to access memory that is out of scope? – 8protons May 11 '18 at 06:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170827/discussion-between-basile-starynkevitch-and-8protons). Please join that chat, we need to discuss interactively. You are still confused. – Basile Starynkevitch May 11 '18 at 06:39
  • 1
    Ain't this answer the truth. It is hard to imagine that an operating systems book in use today would cover base and limit registers beyond being a historical step is amazing. – user3344003 May 13 '18 at 00:46