summaryrefslogtreecommitdiff
path: root/doc/8.md
blob: 415e61d0e5caf16a2ee575c8d5522f2d678665dc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# Chapter 8 - Main Memory


The `base register` holds the smallest legal physcial memory address;
The `limit register` specifies the size of the range.

E.g.

accessible process memory (inclusive)
* base: 300040
* limit: 120900

```plaintext
          ---------------------------|
        0 |                          |
          |  Operating System        |
          |                          |
   256000 |--------------------------|
          |                          |
          | process                  |
          |                          |
   300040 |--------------------------| <--- (300040) base
          |                          |
          | process                  |
          |                          |
   420940 |--------------------------| <--- (120900) limit
          |                          |
          | process                  |
          |                          |
   880000 |--------------------------|
          |                          |
          |                          |
          |                          |
  1024000 |--------------------------|
```


Protection of memory space is accomplished by having the CPU hardware compare
every address generated in user mode with registers.
Any attempt by a program executing in user mode to access OS memory or
other users' memory results in a trap to the operating system, which treats the
attempt as fatal error.

This scheme prevents a user program from modifying code of data structures of
either the operating system or other users.

The base and limit registers can only be loaded by the operating system which uses
a special privileged instruction. Priviliged instructions can only be executed in
kernel mode by the operating system.

An operating system that provides multiprocessing must store the process state
and load the process state when context switching.

## Address Binding

Usually, a program resides on a disk as a binary executable file.
To be executed the program must be loaded into memory and placed within a process.

The processes on the disk that are waiting to be brought into memory for execution form
the `input queue`. When a process terminates its memory space is marked as available.

Addresses in the source program are generally symbolic. A compiler typically `binds` these
symbolic addresses to relocatable addresses (such as 14 bytes from the beginning of this module).
The linkage editor or loader in turn binds the relocatable addresses to absolute addresses (such as
74014).

Each binding is a mapping from one address space to another.

* Compile time binding: `absolute code` is generated to use hardcoded addresses.
  * MS-DOS COM format does this.
  * If location of address changes then the program is broken.
* Load time binding: The compiler generates `relocatable code` where final binding is delayed until load time.
  * If the starting address changes, then we need to reload the user code to adjust.
* Execution time binding: binding can be delayed until run time but special hardware must be available for this scheme to work.

## Logical vs. Physical Address Space

CPU generated addresses are referred to as a `logical address` or `virtual address`.
An address seen by the memory unit, i.e. loaded into the `memory-address register`,
is called `physcial address`.

The compile-time and load-time address-binding methods generate identical logical and physical addresses.
Execution-time addresses result in differing logical and physical addresses.

```plaintext
                                                                         ------------
                                                                         |          |
                                                                         |          |
                                |------------|                           |          |
                                | relocation |                           |          |
                                | register   |                           |          |
                                |            |                           |          |
                                | | 14000 |  |                           |          |
                                |            |                           |          |
-------                         |     _      |                           |          |
| CPU | --> logical address --> |    (+)     | ---> physical address --> |  memory  |
-------          346            |     -      |           14346           |          |
                                |            |                           |          |
                                |            |                           |          |
                                |------------|                           |          |
                                     MMU                                 |          |
                                                                         |          |
                                                                         |          |
                                                                         ------------
```

The `memory-management unit (MMU)` maps virtual to physical addresses.

The `base register` is referred to as the `relocation register` in the virtual address scheme.