Professional Documents
Culture Documents
ECE391 - Ch4 - Memory Management
ECE391 - Ch4 - Memory Management
Memory Management
Ensured
by
⚫Multi User Operating Systems Segmenta
⚫Ease of Programming tion
⚫Process Mobility in the Address Space
⚫Multiprocess Context switching
⚫Protection across Processes Ensured
⚫Intra process protection: Separation of Code,
by
Data and Stack Paging
⚫Inter process protection
⚫Virtual Memory
⚫4GB address space for every process
2
9/4/
1. Memory Segmentation
Main Memory
1. Memory Segmentation
Main Memory
Multiple Segments
⚫The segment register can change its values to point to different
segments at different times.
⚫X86 architecture provides additional segment registers to access
multi data segments at the same time.
⚫DS, ES, FS and GS
⚫X86 supports a separate Stack Segment Register (SS) and a Code
segment Register (CS) in addition.
⚫By default a segment register is fixed for every instruction, for all
the memory access performed by it. For eg. all data accessed by
MOV instruction take DS as the default segment register.
⚫An segment override prefix is attached to an instruction to
change the segment register it uses for memory data access.
Multiple Segments
⚫Multiple segments provide hardware enforced protection of code, data
structures, and programs and tasks.
6
9/4/
0000
mov [10], eax
- this will move the DS
contents of eax register to
memory location 0510 0500
Opcode: 0x89 0x05 0x10
mov [ES:10], eax
C 1500
-this will move the contents
of eax register to memory S
location 3510
Opcode SS 2500
0x26 0x89 0x05 0x10
“0x26” is the segment
override prefix. 3500
E
S
Multiple Segments
7
Process 1
C CS
S Process 1
DS
Process 1 D Process 2
in S CS
Execution Process 2
SS
S
Process 2
S DS
Process 2 Process 1
SS
in
Execution
Multiprocess Context switching
8
9/4/
Operating mode
⚫Intel processor runs in five modes of operations
⚫Real Mode: 20-bit segmented memory address space, no
support for memory protection, multitasking, or code privilege
levels
⚫Protected Mode: allows system software to use features such
as virtual memory, paging and safe multi-tasking
⚫Virtual 8086 mode is used to run 8086 compatible programs
concurrently with other protected mode programs
⚫Long mode– Extended Memory model for 64-bits
⚫Compatibility mode – execute 32 bit code in 64-bit mode
without recompilation. No access to 64-bit address space
⚫64-bit mode – 64-bit OS accessing 64-bit address space and
64-bit registers
⚫System Management mode (80386): provide for handling
system-wide functions like power management, system
hardware control, or proprietary OEM designed code
10
9/4/
12
9/4/
Descriptor Table
Linear
Address
13
2. Protection
⚫In the protected mode, x86 processor provide a
protection mechanism operates at both the segment
level and the page level.
⚫The protection checks that are performed fall into the
following categories:
⚫Limit checks.
⚫Type checks.
⚫Privilege level checks.
14
9/4/
15
500
CS Every segment is specified a
1000
start address and limit.
ES 1500 Architecture checks if limit is
not exceeded.
SS 2000
jmp
mov jmp
PUSH
CS:501
POP
PUSH CS:250
[ES:498],
POP
mov
EAX
EAX
AX
[ES:498],
//This
AX //Let
//Let
//Let
EAXis SP
aSP
SP//This
AX
//This
violation
be
be
be
//This
2,498,
498,
2, itaisasfine
isViolation!!!
it
is
violation!!!
violation
is
fine
limit
fineis 500
Interprocess Protection
17
18
9/4/
19
20
9/4/
21
22
9/4/
23
Protection Implementation
⚫Every segment is associated with a descriptor
stored in a descriptor table.
⚫ The privilege level of any segment is stored in
its descriptor.
⚫ The descriptor table is maintained in memory
and the starting location of the table is pointed
to by a Descriptor Table Register (DTR).
⚫ The segment register stores an offset into this
table.
24
9/4/
Structure of a Descriptor
25
26
9/4/
27
Privilege levels
⚫The need is to prevent
⚫Users from interfering with one another
⚫Users from examining secure data
⚫Program bugs from damaging other programs
⚫Program bugs from damaging data
⚫Malicious attempts to compromise system integrity
⚫Accidental damage to data
28
9/4/
Privilege Protection
⚫Continuous checking by the processor on
whether the application is privileged enough to
⚫Type 1: Execute certain instructions
⚫Type 2: Reference data other than its own
⚫Type 3: Transfer control to code other than its own
⚫To manage this every segment has a privilege
level called the DPL (Descriptor Privilege
Level) Bits 45,46
29
3. Virtual memory
⚫What if we wanted more RAM than we
had available. For example, we might have
1 M of RAM, what if we wanted 10 M? How
could we manage?
⚫One way to extend the amount of memory
accessible by a program is to use disk. This
idea of extending memory is called
virtual memory. It's called "virtual" only
because it's not RAM.
⚫The real problem with disk is that it's
really, really slow to access. The advantage
of disk is it's easy to get lots of disk space
for a small cost.
⚫Still, because disk is so slow to access, we
want to avoid accessing disk unnecessarily.
30
9/4/
31
Virtual
address
space
⚫A virtual address
space is the set
of ranges of
virtual addresses
that an operating
system makes
available to a
process
32
9/4/
33
Paging fundamentals
⚫A page is a sequence of N bytes where N is a
power of 2.
⚫In 8088/8086 processors:
⚫Each page is 4096 bytes
⚫Physical RAM has page frames like photo frames,
which is also 4096 bytes.
⚫A page is copied into the page frame, if needed and
removed to accommodate some other page.
⚫By this, a 4 GB code can run on a 128MB physical
memory
⚫In modern processors:
⚫Page sizes are at least 4K in size and maybe as large as
64 K or more.
34
9/4/
PAGE FRAME
Paging - Example
⚫Suppose your program generated the following virtual
address F0F0F0F0hex (which is 1111 0000 1111 0000 1111 0000
1111 0000 two). How would you translate this to a physical
address?
36
9/4/
37
38
9/4/
39
⚫New
⚫P= new (sizeof(char));
⚫my_class = new Myclass* [size1];
⚫Example:
40
9/4/
41
⚫Example:
void f(int n)
{ int* array = calloc(n, sizeof(int));
//do_some_work(array);
free(array);
}
42
9/4/
struct MyClass {
int data[100];
MyClass() {std::cout << "constructed [" << this << "]\n";}
};
int main () {
std::cout << "1: ";
MyClass * p1 = new MyClass;
// allocates memory by calling: operator new (sizeof(MyClass))
// and then constructs an object at the newly allocated space
44
9/4/
⚫Example
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char name[100];
char *description;
strcpy(name, "Zara Ali");
description = malloc( 30 * sizeof(char) ); /* allocate memory dynamically */
if( description == NULL ) {
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else {
strcpy( description, "Zara ali a DPS student.");
}
/* suppose you want to store bigger description */
description = realloc( description, 100 * sizeof(char) );
if( description == NULL ) {
fprintf(stderr, "Error - unable to allocate required memory\n");
}
else {
strcat( description, "She is in class 10th");
}
printf("Name = %s\n", name );
printf("Description: %s\n", description );
46