ce6105 linux 作業系統 linux operating system 許 富 皓

73
CE6105 Linux 作作作作 Linux Operating System 作 作 作

Upload: zeus-davis

Post on 30-Dec-2015

59 views

Category:

Documents


0 download

DESCRIPTION

CE6105 Linux 作業系統 Linux Operating System 許 富 皓. Sharing Process Address Space. Reduce memory usage (e.g. editor.) Explicitly requested by processes (e.g. shared memory for interprocess communication.) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

CE6105

Linux 作業系統

Linux Operating System

許 富 皓

Page 2: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Sharing Process Address Space

Reduce memory usage (e.g. editor.)Explicitly requested by processes (e.g. shared memory for interprocess communication.)mmap() system call allows part of a file or the memory residing on a device to be mapped into a part of a process address space.

Page 3: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Race Condition

When the outcome of some computation depends on how two or more processes are scheduled, the code is incorrect. We say that there is a race condition.Example:

Variable v contains the number of available resources.

Page 4: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Critical Region

Any section of code that should be finished by each process that begins it before another process can enter it is called a critical region.

Page 5: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Synchronization

Atomic Operation: a single, non-interruptible operationnot suitable for complex operation (e.g. delete a node from a linked list.)

Page 6: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

SynchronizationNonpreemptive kernels

When a process executes in kernel mode, it cannot be arbitrarily suspended and substituted with another process. Therefore on a uniprocessor system, all kernel data structures that are not updated by interrupts or execption handlers are safe for the kernel to access.Ineffective in multiprocessor system.

Page 7: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

SynchronizationInterrupt Disabling:

Disabling interrupts before entering critical region and restoring the interrupts after leaving the region.Not efficientNot suitable for multiprocessors.

Page 8: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

SynchronizationSemaphore:

Consist of an integer variable, a list of waiting processes, and two atomic methods down() and up().Will block process; therefore, it is not suitable for interrupt handler.

Page 9: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

SynchronizationFor multiprocessor system:

When time to update the data protected by semaphores is short, then semaphores are not efficient.When a process finds the lock closed by another process, it spins around repeatedly, executed a tight instruction loop until the lock becomes open.

Page 10: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

SynchronizationAvoid Deadlock.

Page 11: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Signals

Linux uses signals to notify processes system events:

Asynchronous notifications

Synchronous errors or exceptions

Page 12: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Signal Notification

Asynchronous: CTRL-C SIGINT.

Synchronous (error and exception): e.g. access an illegal address SIGSEGV.

Page 13: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Processes’ Responses to Signals

Ignore.

Asynchronously execute a signal handler.

Signal SIGKILL and SIGSTOP can not be directly handled by a process or ignored.

Page 14: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Kernel Default Actions to Signals

When a process doesn’t define its response to a signal, then kernel will utilize the default action of the signal to handle it.

Each signal has its own kernel default action.

Page 15: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Kernel Default Actions to Signals

Terminate the Process.

Core dump and terminate the process

Ignore

Suspend

Resume, if it was stopped.

Page 16: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Process Management-related System Calls

fork()Duplicate a copy of the caller process.

Caller parent

New process child_exit()

Send a SIGCHLD signal to the exiting process’s parent process.

The signal is ignored by default

exec()Copy-On-Write (COW)

Page 17: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Zombie Process

A terminated process whose parent process has not executed a wait() system call on it.

Instead of using signal SIGCHLD to reclaim the resource of a terminated process, the parent process uses wait() system call to finish the job.

Page 18: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Process initinit is a special system process which is created during system initialization./etc/inittabgettylogin shell

If a parent process terminates before its child process(es) does (do), then init becomes the parent process of all those child process(es).

Page 19: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

ShellAlso called a command line interpreter.When you login a system, it displays a prompt on the screen and waits for you to enter a commend. A running shell is also a process.Some of the famous shells

Bourne shell (/bin/sh)Bourne Again shell (/bin/bash)Korn Shell (/bin/ksh)C-shell (/bin/csh)

Page 20: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Chapter 2

Memory Addressing

Page 21: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Logical Addresses

Logical address:Used in machine language instructions to specify the address of an instruction or an operand.A logical address segment base address + offset

• offset: the distance from the start of the segment to the actual address.

• In an assembly language instruction, the segment base address part is stored in a segment register and is usually omitted, because most segments are specified by default segment registers:

e.g. code segments use cs register.

Page 22: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Linear Addresses

Linear Address (Virtual Address)In a IA-32 architecture, it is a unsigned 32-bit integer.

232 = 4 Giga bytes

From 0x00000000 to 0xffffffff

Page 23: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Physical AddressPhysical address

Used to address memory cells in memory chips.

Signals appear on the address bus and CPU’s address pins.

Physical addresses are also represented by a 32-bit unsigned integer.

Page 24: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Physical Memory AddressesMemory chips consist of memory cells. Each memory has a unique address.Each memory cell is one byte long.Memory cells may contain instructions or data.

Page 25: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Programs use a memory address to access the content of a memory cell.

The address used by physical memory is different from the address used in a program, even though both are 32-bit unsigned integers.

Memory Addresses Used in a Program – Logical Addresses

Page 26: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Logical Address Example

main: pushl %ebp movl %esp, %ebp subl $8, %esp andl $-16, %esp movl $0, %eax subl %eax, %esp movl $3, -4(%ebp) movl $2, -8(%ebp) leave ret

main()

{

int a,b;

a=3;

b=2;

}

offset

Page 27: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Address Transformation

Segmentation UnitA hardware circuit

Transform a logical address into a virtual address.

Paging Unit:A hardware circuit

Transform a virtual address into a physical address.

Page 28: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Logical Address Translation

inside a CPU

Segmentation Unit

Paging Unit

Page 29: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Intel 80386 Data Flow

Page 30: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Memory Arbitrator

When multiple processors could access the same memory chips, a memory arbitrator guarantees that at any instance only one processor could access a chip.

A multiprocessor system

DMA

Resides between the address bus and memory chips.

Page 31: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

CPU Mode

Starting for 80386, Intel provides two logical address translation method.

Real Mode• Compatibility with older processors• bootstrap

Protected Mode• In this chapter we only discuss this mode.

Page 32: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Segmentation Unit

A logical address is decided by a16-bit segment selector (segment identifier) and a 32-bit offset within the segment identified by the segment selector.

Page 33: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Segment Registers

An IA-32 processor has 6 segment registers (cs, ss, ds, es, fs, gs)Each segment register holds a segment selector.

cs: points to a code segment ss: points to a stack segmentds: points to a data segment.es, fs, and gs: general purpose segment register may point to arbitrary data segments.

Page 34: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

CPU Privilege Levels

The cs register includes a 2-bit field that specifies the Current Privilege Level (CPL) of the CPU. The value 0 denotes the highest privilege level, while the value 3 denotes the lowest one.

Linux uses only levels 0 and 3, which are respectively called Kernel Mode and User Mode.

Page 35: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

The addresses used by a program are divided into several different areas (segments). Items used by a program with similar properties are saved in the same segment.

Each segment is represented by an 8-byte Segment Descriptor that describes the segment characteristics.

Segment Descriptors

Page 36: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

GDT vs. LDT

Segment Descriptors are stored either in the Global Descriptor Table (GDT ) or in the Local Descriptor Table (LDT ).

Usually only one GDT is defined, while each process is permitted to have its own LDT if it needs to create additional segments besides those stored in the GDT.

Page 37: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

gdtr and ldtr

The CPU register gdtr contains the address of the GDT in main memory.

The CPU register ldtr contains the address of the LDT of the currently used LDT.

Page 38: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Segment Descriptor FormatBase field (32): the linear address of the first byte of the segment.G granularity flag (1): 0 (byte); 1 (4K bytes).Limit field (20).S system flag (1): 0 (system segment); 1 (normal segment).Type field (4): segment type and its access rights.DPL (Descriptor privilege level) (2):Segment-present flagD/B flagReserved bitAVL flag

Page 39: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Frequently Used Segment Descriptor Types

Code Segment Descriptor.

Data Segment Descriptor.P.S.: Stack Segments are implemented by means of Data Segment Descriptors.

Task State Segment Descriptor (TSSD)A TSSD describes a Task State Segment (TSS) which is used to store the contents of a process registers.

Local Descriptor Table Descriptor (LDTD)

Page 40: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Segment Descriptors

Page 41: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Segment Selector Format

Page 42: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Segment Registers

Each segment register contain a segment selector.

13-bit index1-bit TI (Table Indicator) flag.2-bit RPL (Requestor Privilege Level)

• The cs register’s RPL also denotes the current privilege level of the CPU.

• 0 represents the highest privilege. Linux uses 0 to represent the kernel mode and 3 to represent the user mode.

Associated with each segment register is an additional nonprogrammable register which contain the segment descriptor specified by the segment selector.

Page 43: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

DPL (Descriptor Privilege Level)

2-bit field used to restrict access to the segment. It represents the minimal CPU privilege level requested for accessing the segment.

Page 44: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Locate the Segment Descriptor Indicated by Segment Selector

address=(gdtr/ldtr) + index*8.

The first entry of the GDT is always 0.

The maximum number of segment descriptors that the GDT can have is 213-1.

Page 45: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Fast Access to Segment Descriptor

Page 46: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Translation of a Logical Address

OffsetSelector

Page 47: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Segmentation in Linux

Page 48: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Segmentation in LinuxAll Linux processes running in User Mode use the same pair of segments to address instructions and data. These segments are called user code segment and user data segment, respectively. Similarly, all Linux processes running in Kernel Mode use the same pair of segments to address instructions and data: they are called kernel code segment and kernel data segment, respectively. Under the above design, it is possible to store all segment descriptors in the GDT.

Page 49: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Metaphor for a Segment DescriptorLike method used to represent a location in a blueprint.

E.g. • Method 1: at which location of which floor• Method 2: height, length, width• … and so on.

Different house (comparing with a process) could use the same method (comparing with a segment descriptor table) to describe a location in its blueprint. Hence, in the blueprint of a house the notations used to indicate a place are the same as all other houses; however, each place in a blueprint represents a different physical place.

Page 50: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Values of the Segment Descriptor Fields for the Four Main Linux Segments

The corresponding Segment Selectors are defined by the macros __USER_CS, __USER_DS, __KERNEL_CS, and __KERNEL_DS, respectively.

To address the kernel code segment, for instance, the kernel just loads the value yielded by the __KERNEL_CS macro into the cs segmentation register.

Page 51: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Linux Logic Addresses and Linear Addresses

The linear addresses associated with such segments all start at 0 and reach the addressing limit of 232 -1. This means that all processes, either in User Mode or in Kernel Mode, may use the same logical addresses.Another important consequence of having all segments start at 0x00000000 is that in Linux, logical addresses coincide with linear addresses; that is, the value of the Offset field of a logical address always coincides with the value of the corresponding linear address.

Page 52: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Privilege Level Change

The RPL of CS register determine the current privilege level of a CPU; hence, when the CS is changed all corresponding DS, SS registers must also be changed.

Page 53: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

The Linux GDT

Page 54: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

The Linux GDT

In uniprocessor systems there is only one GDT, while in multiprocessor systems there is one GDT for every CPU in the system.

All GDTs are stored in the per-CPU cpu_gdt_table[1],[2],[3],[4] array, while the addresses and sizes of the GDTs (used when initializing the gdtr registers) are stored in the cpu_gdt_descr [5],[6] array.

Page 55: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

GDT Layout

Each GDT includes 18 segment descriptors and 14 null, unused, or reserved entries.

Unused entries are inserted on purpose so that Segment Descriptors usually accessed together are kept in the same 32-byte line of the hardware cache.

Page 56: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Linux’s GDT

Linux’s GDT Linux’s GDT

Page 57: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Data Structure of a GDT Entry

In Linux, the data type of a GDT entry is struct desc_struct.

struct desc_struct

{

unsigned long a,b;

};

Page 58: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Task State Segment

In Linux, each processor has only one TSS.

The virtual address space corresponding to each TSS is a small subset of the liner address space corresponding to the kernel data segment.

Page 59: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Task State SegmentAll the TSSs are sequentially stored in the per-CPU init_tss variable

struct tss_struct { unsigned short back_link,__blh; unsigned long esp0; unsigned short ss0,__ss0h; unsigned long esp1; unsigned short ss1,__ss1h; unsigned long esp2; unsigned short ss2,__ss2h; unsigned long __cr3, eip,eflags; unsigned long eax,ecx,edx,ebx; unsigned long esp, ebp, esi, edi; unsigned short es, __esh, cs, __csh, ss, __ssh, ds, __dsh; unsigned short fs, __fsh, gs, __gsh, ldt, __ldth; unsigned short trace, bitmap; unsigned long io_bitmap[IO_BITMAP_LONGS + 1]; unsigned long io_bitmap_max; struct thread_struct *io_bitmap_owner; unsigned long __cacheline_filler[35]; unsigned long stack[64]; };

A TSS

Page 60: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Task State Segment

The TSS descriptor for the nth CPUThe Base field: point to the nth component of the per-CPU init_tss variable.

G flag: 0

Limit field: 0xeb (each TSS segment is 236 bytes)

DPL: 0

Page 61: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Thread-Local Storage (TLS) Segments

Three Thread-Local Storage (TLS) segments: this is a mechanism that allows multithreaded applications to make use of up to three segments containing data local to each thread.

The set_thread_area( ) and get_thread_area( ) system calls, respectively, create and release a TLS segment for the executing process.

Page 62: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Other Special Segments

Three segments related to Advanced Power Management (APM ).

Five segments related to Plug and Play (PnP ) BIOS services.

A special TSS segment used by the kernel to handle "Double fault " exceptions.

Page 63: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

GDTs of Different CPUsThere is a copy of the GDT for each processor in the system. All copies of the GDT store identical entries, except for a few cases:

First, each processor has its own TSS segment, thus the corresponding GDT's entries differ. Moreover, a few entries in the GDT may depend on the process that the CPU is executing (LDT and TLS Segment Descriptors). Finally, in some cases a processor may temporarily modify an entry in its copy of the GDT;

• this happens, for instance, when invoking an APM's BIOS procedure.

Page 64: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Local Descriptor Table (LDT)

A default LDT is usually shared by ALL processes.

The segment that store the default LDT is the default_ldt variable.

struct desc_struct default_ldt[];

default_ldt includes five entries.

Page 65: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Contents of GDT for Processor n

Linux’s GDT Linux’s GDT

per-CPU init_tss

n-1

default_ldt

Page 66: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Per-CPU Variables

Page 67: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

typeof Operator [IBM]

The typeof operator returns the type of its argument, which can be an expression or a type.The language feature provides a way to derive the type from an expression. The typeof operator is an orthogonal language extension provided for handling programs developed with GNU C. The alternate spelling of the keyword, __typeof__, is recommended.Given an expression e, __typeof__(e) can be used anywhere a type name is needed,

for example in a declaration or in a cast.

Page 68: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Example (1)

int e;

__typeof__(e + 1) j; /* the same as declaring int j; */

e = (__typeof__(e)) f; /* the same as casting e = (int) f; */

Page 69: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Example (2)

Given int T[2];

int i[2];

you can write __typeof__(i) a; /* all three constructs have the same meaning */

__typeof__(int[2]) a;

__typeof__(T) a;

The behavior of the code is as if you had declared

int a[2];.

Page 70: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Comma ExpressionsA comma expression contains two operands of any type separated by a comma and has left-to-right associativity. The left operand is fully evaluated, possibly producing side effects, and its value, if there is one, is discarded. The right operand is then evaluated. The type and value of the result of a comma expression are those of its right operand, after the usual unary conversions.

Page 71: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Example (1)

The following statements are equivalent:

r = (a,b,...,c);

a; b; r = c; 

Page 72: CE6105 Linux 作業系統 Linux Operating System  許 富 皓

Example (2)

&(a, b)

a, &b

Page 73: CE6105 Linux 作業系統 Linux Operating System  許 富 皓