Implementing system calls in the Y86 model Soumava Ghosh.

Slides:



Advertisements
Similar presentations
CS 4284 Systems Capstone Godmar Back Processes and Threads.
Advertisements

Machine/Assembler Language Putting It All Together Noah Mendelsohn Tufts University Web:
Chapter 6 Limited Direct Execution
The ‘system-call’ interface We see how an application program can invoke privileged kernel services.
Context Switch Animation Another one by Anastasia.
Memory Image of Running Programs Executable file on disk, running program in memory, activation record, C-style and Pascal-style parameter passing.
1 Function Calls Professor Jennifer Rexford COS 217 Reading: Chapter 4 of “Programming From the Ground Up” (available online from the course Web site)
Chapter 12: High-Level Language Interface. Chapter Overview Introduction Inline Assembly Code C calls assembly procedures Assembly calls C procedures.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
1 System Calls and Standard I/O Professor Jennifer Rexford
CSE 451 Section 4 Project 2 Design Considerations.
1. 2 FUNCTION INLINE FUNCTION DIFFERENCE BETWEEN FUNCTION AND INLINE FUNCTION CONCLUSION 3.
September 22, 2014 Pengju (Jimmy) Jin Section E
POSIX: Files Introduction to Operating Systems: Discussion 1 Read Solaris System Interface Guide: Ch. 5.1 Basic File I/O.
1 CSC 2405: Computer Systems II Spring 2012 Dr. Tom Way.
System Calls 1.
Y86 Processor State Program Registers
Dr. José M. Reyes Álamo 1.  The 80x86 memory addressing modes provide flexible access to memory, allowing you to easily access ◦ Variables ◦ Arrays ◦
OPERATING SYSTEM OVERVIEW. Contents Basic hardware elements.
Code Generation Gülfem Savrun Yeniçeri CS 142 (b) 02/26/2013.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
UNIX Files File organization and a few primitives.
Low Level Programming Lecturer: Duncan Smeed The Interface Between High-Level and Low-Level Languages.
CSCI 330 UNIX and Network Programming Unit VII: I/O Management I.
1 Understanding Pointers Buffer Overflow. 2 Outline Understanding Pointers Buffer Overflow Suggested reading –Chap 3.10, 3.12.
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
Information Security - 2. A Stack Frame. Pushed to stack on function CALL The return address is copied to the CPU Instruction Pointer when the function.
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures.
1 Assembly Language: Function Calls Jennifer Rexford.
ROP Exploit. ROP Return Oriented Programming (ROP): is a hacking exploit technique where you exploit buffer overflow to inject a chain of gadgets. Each.
1 COMP 3500 Introduction to Operating Systems Project 4 – Processes and System Calls Overview Dr. Xiao Qin Auburn University
NASM ASSEMBLER & COMPILE WITH GCC 어셈러브 refered to ‘PC Assembly Language’ by Paul A. Carter
Using System Calls (Unix) Have to tell compiler (if C/C++) where to find the headers, etc. – i.e., the “include” files May have to tell compiler where.
Files in UNIX u UNIX deals with two different classes of files:  Special Files  Regular Files u Regular files are just ordinary data files on disk -
File System Design David E. Culler CS162 – Operating Systems and Systems Programming Lecture 23 October 22, 2014 Reading: A&D a HW 4 out Proj 2 out.
Virtualizing the CPU: Processes 1. How to provide the illusion of many CPUs? CPU virtualizing – The OS can promote the illusion that many virtual CPUs.
CPSC 121: Models of Computation
Practical Session 3.
CS 177 Computer Security Lecture 9
Instructions for test_function
Assembly function call convention
Computer Architecture and Assembly Language
Assembly language.
C function call conventions and the stack
Processes.
Conditional Branch Example
Protection of System Resources
Anton Burtsev February, 2017
Homework Reading Machine Projects Labs PAL, pp ,
Exploiting & Defense Day 2 Recap
Homework In-line Assembly Code Machine Language
High-Level Language Interface
Assembly Language Programming V: In-line Assembly Code
Computer Architecture adapted by Jason Fritts then by David Ferry
asum.ys A Y86 Programming Example
Y86 Processor State Program Registers
Introduction to Intel IA-32 and IA-64 Instruction Set Architectures
Assembly Language Programming II: C Compiler Calling Sequences
CSE 451: Operating Systems Spring 2012 Module 6 Review of Processes, Kernel Threads, User-Level Threads Ed Lazowska 570 Allen.
Discussions on HW2 Objectives
Practical Session 4.
Machine Level Representation of Programs (IV)
Multi-modules programming
System Calls David Ferry CSCI 3500 – Operating Systems
Discussions on HW2 Objectives
System Calls System calls are the user API to the OS
Computer Architecture and System Programming Laboratory
Return-to-libc Attacks
Presentation transcript:

Implementing system calls in the Y86 model Soumava Ghosh

System Calls The standard method for user mode programs to request services from the OS kernel. Execution involves a change in privilege level, and transfer of control to the kernel which executes certain instructions as per the programs requirement and returns control to the user mode (with the desired result).

System Calls (cont.) open() User Mode program calls open() Subroutine call to __libc_open syscall/sysenter __libc_open is the libc wrapper Multiple macros expand to invoke assembly code that contains the syscall instruction … Faster transfer of control to kernel (as compared to earlier INT 80H) System call id (__NR_open = 3) expected in %eax, other parameters in %edi, %esi, %edx registers. sysret/sysexit Kernel finishes up its work and calls sysret, that returns the control back to the user mode.

Modeling system calls Programs that run on the Y86 model are user mode programs Need a mechanism to interact with the kernel from ACL2, and retrieve results Will not be modeling the kernel mode code that performs the actual action of the system call – our syscall instruction will be a combination of the actual syscall (UM) + sysret (KM).

Options considered ACL2 sys-call: – Executes system commands as in a shell – Not actually a direct system call – Does not support interactive input CLISP modules: – POSIX and OS modules have methods that map to most Unix system calls – Preferred Common Lisp for ACL2 is CCL, which wouldn’t have these modules - so a CLISP option wouldn’t be the best solution

Options considered (contd.) CCL Foreign Function Interface (FFI) – Enables Common Lisp code to call functions outside of Lisp (e.g. C libraries) – FFI interface translator provides information about the entry point, argument and return types – Lisp data should be copied to a foreign representation before calling the foreign function (eq. string  pointer) – Supports interactive console and file input

CLISP Foreign Function Interface Load libraries (ccl::open-shared-library “/path/to/library.dylib”) # List entry points (ccl::external “_write”) # Convert to foreign data types (setq ptr (ccl::make-cstring “xyz”)) #

CCL FFI (contd.) Allocate memory for reading in data (multiple-value-bind (lstr lptr) (ccl::make-heap-ivector nbytes '(unsigned-byte 8)) (setq str lstr) (setq ptr lptr)) Invoke entry points (external-call “_write” :unsigned-int 1 :address ptr :unsigned-int nbytes) Hello NIL

Making system calls from ACL2 Every POSIX system has the syscall() API defined as below: int syscall (int number,...); The first parameter indicates the system call id, and the subsequent variable arguments are mandatory arguments of the particular system call

Making system calls from ACL2 (contd.) Observation: It looks like loading the system library is not required as it was found to always be loaded. This works out well as the name and location of the system library could differ across systems.

The prototype Initial prototype includes the 4 most basic system calls: – int open(const char *path, int oflags) – int close(int filedes) – size_t read(int fildes, void *buf, size_t nbytes) – size_t write(int fildes, const void *buf, size_t nbytes) Raw lisp code was written to use the CCL FFI to invoke the system calls and return the required data in a native lisp format.

The prototype (contd.) Example: the write() system call ;; size_t write(int fildes, const void *buf, size_t nbytes) (defun syscall-write (clk filedes buffer nbytes) (declare (ignore clk)) (setq ptr (ccl::make-cstring buffer)) (setq ret (ccl::external-call "syscall" :unsigned-int 1 :unsigned-int filedes :address ptr :unsigned-int nbytes :unsigned-int)) (ccl::dispose-heap-ivector ptr) (cons ret nil))

The prototype (contd.) The ACL2 interface consists of a stub definition of the same methods as the common lisp ones. The model restricts the theorem prover from proving anything else other than the theorems that have been explicitly stated about the stubs. (or more with Matt’s latest work) Matt will talk about this part in detail later.

Integrating with the Y86 model The syscall instruction requires the arrangement of parameters in the following format: – System call id in %eax – Parameters in %edi, %esi, %edx respectively An instruction #xD0 was added to the Y86- basic model. On %eip = #xD0, the y86-step function calls into the y86-syscall method, the syscall handler

Integrating with the Y86 model (contd.) The syscall handler reads %eax and calls the appropriate Y86 system call method. Example: (Y86-syscall-open clk x86-32) – %edi  pointer to file path – %esi  flags – retrieve the actual null-terminated path string – call the ACL2 interaface (syscall-open) – set the return value to %eax

Integrating with the Y86 model (contd.) Other minor changes: – Changes to the recognizer: y86-prog – Changes to the byte code writer: y86-asm

(defun y86-syscall-open (clk x86-32) (declare (xargs :stobjs (x86-32) :guard (natp clk))) (b* ((pc (eip x86-32)) ;; Memory Probe ((if (< *2^32-2* pc)) (!ms (list :at-location pc :instruction 'syscall-open :memory-probe nil :reason 'pc-overflow) x86-32)) (path-ptr (rgfi *mr-edi* x86-32)) ;; Path-ptr sanity check ((if (< *mem-size-in-bytes* path-ptr)) (!ms (list :at-location pc :instruction 'syscall-open :reason 'ptr-overflow) x86-32)) (oflags (rgfi *mr-esi* x86-32)) (path (y86-read-string-null-term x86-32 path- ptr)) (ret (syscall-open clk path oflags)) ;; Save return code to eax (x86-32 (!rgfi *mr-eax* (car ret) x86-32)) (x86-32 (!eip (+ pc 1) x86-32))) x86-32)) (defun syscall-open (clk pathname flags) (declare (ignore clk)) (setq ptr (ccl::make-cstring pathname)) (setq ret (ccl::external-call "syscall” :unsigned-int 2 :address ptr :unsigned-int flags :unsigned-int)) (cons ret nil))

Testing the Y86-basic : CAT The most basic CAT implementation in assembly code – Hardcoded file name in memory – Open the file – Read the file, write the bytes read to stdout – If the number of bytes read was less than the number of bytes requested, break the loop – Close the file

(!! cat-code '(cat (pushl %ebp) ; Save superior frame pointer (rrmovl %esp %ebp) ; Set frame pointer (irmovl 2 %eax) ; Set eax = 2 -> syscall-open (irmovl #x3000 %edi) ; Set edi = path ptr (xorl %esi %esi) ; Set esi = 0 (O_RDONLY) (syscall) ; call open to open the file (rrmovl %eax %ebx) ; store the file descriptor ;; Loop while !eof loop (xorl %eax %eax) ; 0 -> eax (rrmovl %ebx %edi) ; file descriptor to edi (irmovl #x4000 %esi) ; buffer location to esi (irmovl #x64 %edx) ; buffer size = 100 to edx (syscall) ; actually call read (rrmovl %eax %edx) ; store readBytes (irmovl 1 %eax) ; 1 -> eax (irmovl 1 %edi) ; file-descriptor of stdout (irmovl #x4000 %esi) ; buffer location to esi (syscall) ; syscall-write (irmovl #x64 %eax) ; Move 100 to eax (subl %edx %eax) ; subtract 100 from readBytes (je loop) ; loop if zero ;; Subroutine close and leave cat_close_leave (irmovl 3 %eax) ; Set eax = 3 (rrmovl %ebx %edi) ; file descriptor to edi (syscall) ; syscall-close ;; Subroutine leave cat_leave (rrmovl %ebp %esp) ; restore stack ptr (popl %ebp) ; restore base ptr (ret) ; return from subroutine ;; Main (align 16) ; align to 16-byte address main ; main program (irmovl stack %esp) ; initialize stack ptr (rrmovl %esp %ebp) ; initialize base ptr (call cat) ; call the cat function (halt) ; halt the machine ;; Stack (pos 8192) ; position 8192 stack ; mark this position as 'stack' ;; String data (pos #x3000) (string "//u//soumava//a//ACL2- devel//books//models//y86//y86- basic//y86//syscalls//systemcalls-raw.lsp") (byte 0) ))

Demo!