Bài giảng An toàn hệ điều hành - Control Flow

Control Flow  
1
Concept of Control Flow  
ꢀ The program counter (PC) assumes a sequence of  
values:  
a1,a2,a3,…,an-1  
ꢀ ak is the address of some corresponding instruction Ik.  
Each transition from ak to ak+1 is called a control  
transfer.  
ꢀ A sequence of such control transfers is called the  
flow of control, or control flow of the processor.  
2
Concept of Exceptional Control Flow  
• The simplest kind of control flow is a smooth sequence where each Ik  
and Ik+1 are adjacent in memory.  
• Ik+1 is not adjacent to Ik, such as jumps, calls, and return=> they are  
necessary mechanisms that allow programs to react to changes in  
internal program state represented by program variables.  
• Other hand, changes in system state that are not captured by internal  
program variables, such as timer, I/O… Modern systems react to  
these changes by making abrupt changes in the control flow. It is  
refered as exceptional control flow.  
• It exists all levels (hardware, operating system, application)  
3
Exceptions  
ꢀ A form of exceptional control flow that are implemented  
partly by the hardware and partly by the operating system.  
ꢀ An exception is an abrupt change in the control flow in  
response to some change in the processor’s state  
4
Exception Handling  
ꢀ Each type of possible exception in a system is assigned a  
unique non-negative integer exception number by the  
designers of the processor and designers of the operating  
system kernel  
ꢀ At system boot time the operating system allocates and  
initializes a jump table called an exception table, so that  
entry k contains the address of the handler for exception k.  
5
6
Classes of Exceptions  
ꢀ Four classes: Interrupts, Traps, Faults, and Aborts  
7
Interrupts  
ꢀ Interrupts occur asynchronously as a result of signals  
from I/O devices  
8
Traps  
ꢀ Traps are intentional exceptions that occur as a result of executing  
an instruction  
ꢀ The most important use of traps is to provide a procedure like  
interface between user programs and the kernel known as a system  
call  
ꢀ Executing the syscall instruction causes a trap to an exception  
handler that decodes the argument and calls the appropriate kernel  
routine  
9
Faults  
ꢀ When a fault occurs, the processor transfers control to the fault  
handler.  
ꢀ If the handler is able to correct the error condition, it returns  
control to the faulting instruction, thereby reexecuting it.  
ꢀ Otherwise, the handler returns to an abort routine in the kernel  
that terminates the application program that caused the fault.  
10  
Aborts  
ꢀ Result from unrecoverable fatal errors  
ꢀ Abort handlers never return control to the application  
program.The handler returns control to an abort routine  
that terminates the application program.  
11  
Exceptions in Intel Processors  
ꢀ Up to 256 different exception types  
ꢀ Numbers in the range 0 to 31 correspond to exceptions  
that are defined by the Pentium architecture.  
ꢀ Numbers in the range 32 to 255 correspond to interrupts  
and traps that are defined by the operating system.  
ꢀ System calls are provided on IA32 systems via a trapping  
instruction called INT n, where n can be the index of any  
of the 256 entries in the exception table. Historically,  
system calls are provided through exception 128 (0x80)  
12  
13  
Processes  
ꢀ An instance of a program in execution.  
ꢀ State includes the program’s code and data stored in  
memory, its stack, the contents of its general-purpose  
registers, its program counter, environment variables,  
and the set of open file descriptors  
ꢀ Each program in the system runs in the context of some  
process. The context consists of the state that the  
program needs to run correctly  
14  
Key abstractions that a process  
provides to the application  
ꢀ An independent logical control flow that provides the  
illusion that our program has exclusive use of the  
processor  
ꢀ A private address space that provides the illusion that our  
program has exclusive use of the memory system.  
15  
Logical Control Flow  
ꢀ A process provides each program with the illusion that it has exclusive  
use (độc quyền sử dụng) of the processor  
ꢀ Sequence of PC values is known as a logical control flow (PC:  
Program Counter)  
ꢀ Concurrent process ; multitasking; time slice  
16  
Private Address Space  
ꢀ A process also provides each program with the illusion that  
it has exclusive use of the system’s address space  
17  
User and Kernel Modes  
ꢀ In order for the kernel to provide an airtight process abstraction  
ꢀ Use a mechanism that restricts the instructions that an application can execute, as  
well as the portions of the address space that it can access.  
ꢀ Controlled by using the mode bit in some control register  
ꢀ Mode bit is set ꢀ process in kernel mode (supersisor mode)  
ꢀ User mode: process is not allowed to execute privileged instructions that do things  
such as halt the processor, change the mode bit, or initiate an I/O operation, and to  
directly reference code or data in the kernel area of the address space  
ꢀ User programs must access kernel code and data indirectly via the system call  
interface  
ꢀ The only way for the process to change from user mode to kernel mode is via an  
18  
exception such as an Interrupt, a fault, or a trapping system call  
Context Switches (1/2)  
ꢀ Context consists of the values of objects such as the general-purpose registers,  
the floating-point registers, the program counter, user’s stack, status registers,  
kernel’s stack, and various kernel data structures such as a page table that  
characterizes the address space, a process table that contains information about  
the current process, and a file table that contains information about the files that  
the process has opened.  
ꢀ The kernel maintains a context for each process. The context is the state that the  
kernel needs to restart a preempted process  
ꢀ The kernel has scheduled a new process to run, it preempts the current process  
and transfers control to the new process using a mechanism called a context  
switch  
Context switch mechanism is built on top of the lower-level exception mechanism  
19  
Context Switches (2/2)  
ꢀ A context switch can occur while the kernel is executing a  
system call on behalf of the user.  
ꢀ A context switch can also occur as a result of an interrupt  
20  
Tải về để xem bản đầy đủ
pdf 32 trang yennguyen 22900 Free
Bạn đang xem 20 trang mẫu của tài liệu "Bài giảng An toàn hệ điều hành - Control Flow", để tải tài liệu gốc về máy hãy click vào nút Download ở trên

File đính kèm:

  • pdfbai_giang_an_toan_he_dieu_hanh_control_flow.pdf