21
Features of Intel Features of Intel Processor Architectures Processor Architectures that Lend to Operating that Lend to Operating System Design System Design Jim Snyder Jim Snyder

Features of Intel Processor Architectures that Lend to Operating System Design Jim Snyder

  • View
    216

  • Download
    0

Embed Size (px)

Citation preview

Features of Intel Processor Features of Intel Processor Architectures that Lend to Architectures that Lend to Operating System DesignOperating System Design

Jim SnyderJim Snyder

OverviewOverview

System CallsSystem Calls Intel Architecture (IA-32) Support Intel Architecture (IA-32) Support

for Interrupts and System Callsfor Interrupts and System Calls System Calls in LinuxSystem Calls in Linux Interrupts in LinuxInterrupts in Linux

System CallsSystem Calls

A A system callsystem call is the mechanism is the mechanism used by an application program to used by an application program to request service from the operating request service from the operating system.system.

Some common examples of system Some common examples of system calls available in Linux are:calls available in Linux are: read, write, open, close, kill or forkread, write, open, close, kill or fork

IA-32 Feature Support for IA-32 Feature Support for Operating System Operating System

DevelopmentDevelopment Memory managementMemory management Protection of software modulesProtection of software modules MultitaskingMultitasking MultiprocessingMultiprocessing Cache managementCache management Hardware resource and power Hardware resource and power

managementmanagement Debugging and performance monitoringDebugging and performance monitoring Exception and interrupt handlingException and interrupt handling

IA-32 Support for IA-32 Support for Interrupts & System Calls Interrupts & System Calls

(1)(1) InterruptsInterrupts Intel 286 Processor (1982)Intel 286 Processor (1982) Privilege levels protect the operating Privilege levels protect the operating

system from:system from: Malicious codeMalicious code Careless codeCareless code

Intel offers privilege levels 0-3Intel offers privilege levels 0-3 Backward compatibilityBackward compatibility

IA-32 Support for IA-32 Support for Interrupts & System Calls Interrupts & System Calls

(2)(2) 2 Types of Interrupts:2 Types of Interrupts:

External hardware interruptsExternal hardware interrupts Software InterruptsSoftware Interrupts

Gates provide needed flexibilityGates provide needed flexibility 4 Types of Gates:4 Types of Gates:

Call gatesCall gates Interrupt gatesInterrupt gates Trap gatesTrap gates Task gatesTask gates

Interrupt Descriptor Table (IDT)Interrupt Descriptor Table (IDT)

Gateways Between High Gateways Between High and Low Privileged Codeand Low Privileged Code

Interrupt and Trap Gate Interrupt and Trap Gate DescriptorsDescriptors

Using IA-32 InterruptsUsing IA-32 Interrupts Interrupt VectorsInterrupt Vectors

Index into IDTIndex into IDT Value of 0-255 (0-31 are reserved)Value of 0-255 (0-31 are reserved)

Hardware InterruptsHardware Interrupts Software Interrupts (relevant Software Interrupts (relevant

instructions)instructions) INT n (most important to system calls in INT n (most important to system calls in

Linux)Linux) INTOINTO INT 3INT 3 BOUNDBOUND

System Calls in Linux (1)System Calls in Linux (1)1.1. InitializationInitialization

Trap_init() is executed within the Trap_init() is executed within the rest_init() function in rest_init() function in init/main.cinit/main.c

Setup_idt() is called from startup_32() Setup_idt() is called from startup_32() in in /arch/i386/kernel/head.S/arch/i386/kernel/head.S

2.2. Invocation of system call in Invocation of system call in programprogram

3.3. Call to libraryCall to library

4.4. SyscallX (SyscallX (include/asm/unistd.hinclude/asm/unistd.h))

Generic MacroGeneric Macro

#define _syscall1(type,name,type1,arg1) \#define _syscall1(type,name,type1,arg1) \type name(type1 arg1) \type name(type1 arg1) \{ \{ \long __res; \long __res; \__asm__ volatile ("int $0x80" \__asm__ volatile ("int $0x80" \

: "=a" (__res) \: "=a" (__res) \: "0" (__NR_##name),"b" ((long)(arg1))); \: "0" (__NR_##name),"b" ((long)(arg1))); \

if (__res >= 0) \if (__res >= 0) \return (type) __res; \return (type) __res; \

errno = -__res; \errno = -__res; \return -1; \return -1; \}}

Example Call & Macro Expansion into AssemblyExample Call & Macro Expansion into Assembly_syscall1(int,chdir,char*,path); _syscall1(int,chdir,char*,path);

_chdir:_chdir: subl $4,%expsubl $4,%exp pushl %ebxpushl %ebx ; save address; save address movzwl 12(%esp),%eaxmovzwl 12(%esp),%eax ; prepare parameters; prepare parameters movl %eax,4(%esp)movl %eax,4(%esp) movl $23,%eaxmovl $23,%eax movl 4(%esp),%ebxmovl 4(%esp),%ebx int $0x80int $0x80 ; software interrupt changes to kernel mode ; software interrupt changes to kernel mode

and jumps to handlerand jumps to handler movl %eax,%edxmovl %eax,%edx testl %edx,%edxtestl %edx,%edx ; check for error; check for error jge L2jge L2 ; if no error, go to L2; if no error, go to L2 negl %edxnegl %edx movl %edx,_errnomovl %edx,_errno movl $-1,%eaxmovl $-1,%eax popl %ebxpopl %ebx addl $4,%espaddl $4,%esp retret L2:L2: movl %edx,%eaxmovl %edx,%eax ; clean up; clean up popl %ebxpopl %ebx addl $4,%espaddl $4,%esp retret ; return; return

Each System Call Has a Each System Call Has a Unique IDUnique ID

From linux/include/linux/unistd.h:From linux/include/linux/unistd.h:

#define __NR_chdir 12#define __NR_chdir 12

#define __NR_time 13#define __NR_time 13

#define __NR_mknod 14#define __NR_mknod 14

#define __NR_chmod 15#define __NR_chmod 15

INT 0x80 Interrupts the KernelINT 0x80 Interrupts the Kernel.align 4.align 4_system_call:_system_call:

pushl %eaxpushl %eax ; save orig_eax; save orig_eaxSAVE_ALLSAVE_ALLmovl $-ENOSYS,EAX(%esp)movl $-ENOSYS,EAX(%esp)cmpl $(NR_syscalls),%eaxcmpl $(NR_syscalls),%eaxjae ret_from_sys_calljae ret_from_sys_callmovl _sys_call_table(,%eax,4),%eaxmovl _sys_call_table(,%eax,4),%eaxtestl %eax,%eaxtestl %eax,%eaxje ret_from_sys_callje ret_from_sys_callmovl _current,%ebxmovl _current,%ebxandl $~CF_MASK,EFLAGS(%esp)andl $~CF_MASK,EFLAGS(%esp) ; clear carry - assume no errors; clear carry - assume no errorsmovl $0,errno(%ebx)movl $0,errno(%ebx)movl %db6,%edxmovl %db6,%edxmovl %edx,dbgreg6(%ebx) movl %edx,dbgreg6(%ebx) ; save current hardware debugging ; save current hardware debugging statusstatustestb $0x20,flags(%ebx)testb $0x20,flags(%ebx)jne 1fjne 1fcall *%eaxcall *%eaxmovl %eax,EAX(%esp)movl %eax,EAX(%esp) ; save the return value; save the return valuemovl errno(%ebx),%edxmovl errno(%ebx),%edxnegl %edxnegl %edxje ret_from_sys_callje ret_from_sys_callmovl %edx,EAX(%esp)movl %edx,EAX(%esp)orl $(CF_MASK),EFLAGS(%esp)orl $(CF_MASK),EFLAGS(%esp) ; set carry to indicate error; set carry to indicate errorjmp ret_from_sys_calljmp ret_from_sys_call

Control Finally TransfersControl Finally Transfersasmlinkage int sys_chdir(const char * filename)asmlinkage int sys_chdir(const char * filename){{ struct inode * inode;struct inode * inode; int error;int error;

error = namei(filename,&inode);error = namei(filename,&inode); if (error)if (error) return error;return error; if (!S_ISDIR(inode->i_mode)) {if (!S_ISDIR(inode->i_mode)) { iput(inode);iput(inode); return -ENOTDIR;return -ENOTDIR; }} if ((error = permission(inode,MAY_EXEC)) != 0) {if ((error = permission(inode,MAY_EXEC)) != 0) { iput(inode);iput(inode); return error;return error; }} iput(current->fs->pwd);iput(current->fs->pwd); current->fs->pwd = inode;current->fs->pwd = inode; return (0);return (0);}}

Interrupts in LinuxInterrupts in Linux

Bidirectional communication:Bidirectional communication: Hardware <-> OSHardware <-> OS

Two types of interrupts under Linux:Two types of interrupts under Linux: Short & longShort & long

All interrupt handlers perform 5 basic actions:All interrupt handlers perform 5 basic actions: Save IRQSave IRQ AcknowledgeAcknowledge Execute interrupt service routineExecute interrupt service routine Terminate by jumping to ret_from_intr()Terminate by jumping to ret_from_intr()

Request_irq() Request_irq() (from (from linux/arch/i386/kernel/irq.clinux/arch/i386/kernel/irq.c))

Struct irqactionStruct irqaction This data structure is found in This data structure is found in include/linux/interrupt.hinclude/linux/interrupt.h

struct irqaction {struct irqaction { void (*handler)(int, void *, struct pt_regs void (*handler)(int, void *, struct pt_regs

*);*); unsigned long flags;unsigned long flags; unsigned long mask;unsigned long mask; const char *name;const char *name; void *dev_id;void *dev_id; struct irqaction *next;struct irqaction *next;};};

ConclusionConclusion From it’s earliest processors back to the From it’s earliest processors back to the

16-bit 8086, released in 1978, Intel has had 16-bit 8086, released in 1978, Intel has had extensive support for hardware and extensive support for hardware and software interrupts. Interrupt features are software interrupts. Interrupt features are vital to operating systems.vital to operating systems.

The only significant architectural change The only significant architectural change

related to interrupts was the inclusion of related to interrupts was the inclusion of the Advanced Programmable Interrupt the Advanced Programmable Interrupt Controller (APIC) in 1993 on the Intel Controller (APIC) in 1993 on the Intel Pentium. The APIC is used in support of Pentium. The APIC is used in support of SMP systems.SMP systems.

ReferencesReferences IA-32 Intel Architecture Software Developer’s IA-32 Intel Architecture Software Developer’s

Manual (volumes 1-3: Basic Architecture, Manual (volumes 1-3: Basic Architecture, Instruction Set Reference & System Instruction Set Reference & System Programming Guide)Programming Guide)

http://futura.disca.upv.es/~eso/en/t2-http://futura.disca.upv.es/~eso/en/t2-arquitectura/gen-t2-arquitectura.htmlarquitectura/gen-t2-arquitectura.html

http://microlabs.cs.utt.ro/~mmarcu/books/03/http://microlabs.cs.utt.ro/~mmarcu/books/03/p_all8.htmp_all8.htm

http://www.cs.ucr.edu/~brett/cs153_w02/http://www.cs.ucr.edu/~brett/cs153_w02/syscall.htmlsyscall.html

http://www.linux.com/guides/lkmpg/x1206.shtmlhttp://www.linux.com/guides/lkmpg/x1206.shtml