systems programming - a6 - iaik

38
Systems Programming A6 Florian Kargl November 22, 2019 IAIK – Graz University of Technology

Upload: others

Post on 10-Jul-2022

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Systems Programming - A6 - IAIK

Systems Programming

A6

Florian Kargl

November 22, 2019

IAIK – Graz University of Technology

Page 2: Systems Programming - A6 - IAIK

A6 - Inline Assembly and Calling Conventions

1 Florian Kargl — IAIK – Graz University of Technology

Page 3: Systems Programming - A6 - IAIK

Course Overview www.tugraz.at

A0, A1, A2

Compiler, C, Multithreading, Synchronization

A3, A4

Virtual Memory, Processes and Sandboxing

A5, A6

System Programming

2 Florian Kargl — IAIK – Graz University of Technology

Page 4: Systems Programming - A6 - IAIK

A6 - Inline Assembly and Calling

Conventions

Page 5: Systems Programming - A6 - IAIK

Function Calls www.tugraz.at

Have you ever wondered what happens in your CPU when you call a function?

Caller

int main()

{

// ...

foo();

// ...

}

Callee

void foo()

{

// do stuff ...

}

3 Florian Kargl — IAIK – Graz University of Technology

Page 6: Systems Programming - A6 - IAIK

Function Calls www.tugraz.at

Let’s take a look at the compiler output

objdump -d <executable>

Caller (ASM)

main:

# ...

call foo

# ...

Callee (ASM)

foo:

# do stuff...

ret

4 Florian Kargl — IAIK – Graz University of Technology

Page 7: Systems Programming - A6 - IAIK

Function Calls www.tugraz.at

Caller (ASM)

main:

# ...

call foo

# ...

Callee (ASM)

foo:

# do stuff...

ret

Stack

%rsp

5 Florian Kargl — IAIK – Graz University of Technology

Page 8: Systems Programming - A6 - IAIK

Function Calls www.tugraz.at

Caller (ASM)

main:

# ...

call foo

# ...

Callee (ASM)

foo:

# do stuff...

ret

Stack

%rspCall instruction pushes return

address onto stack and jumps

to target

5 Florian Kargl — IAIK – Graz University of Technology

Page 9: Systems Programming - A6 - IAIK

Function Calls www.tugraz.at

Caller (ASM)

main:

# ...

call foo

# ...

Callee (ASM)

foo:

# do stuff...

ret

Stack

%rspReturn Addr.

Call instruction pushes return

address onto stack and jumps

to target

5 Florian Kargl — IAIK – Graz University of Technology

Page 10: Systems Programming - A6 - IAIK

Function Calls www.tugraz.at

Caller (ASM)

main:

# ...

call foo

# ...

Callee (ASM)

foo:

# do stuff...

ret

Stack

%rspReturn Addr.

Call instruction pushes return

address onto stack and jumps

to target

5 Florian Kargl — IAIK – Graz University of Technology

Page 11: Systems Programming - A6 - IAIK

Function Calls www.tugraz.at

Caller (ASM)

main:

# ...

call foo

# ...

Callee (ASM)

foo:

# do stuff...

ret

Stack

%rspReturn Addr.

Ret instruction pops return

address from stack and jumps

back

5 Florian Kargl — IAIK – Graz University of Technology

Page 12: Systems Programming - A6 - IAIK

Function Calls www.tugraz.at

Caller (ASM)

main:

# ...

call foo

# ...

Callee (ASM)

foo:

# do stuff...

ret

Stack

%rsp

Return Addr.

Ret instruction pops return

address from stack and jumps

back

5 Florian Kargl — IAIK – Graz University of Technology

Page 13: Systems Programming - A6 - IAIK

Function Calls www.tugraz.at

Caller (ASM)

main:

# ...

call foo

# ...

Callee (ASM)

foo:

# do stuff...

ret

Stack

%rsp

Return Addr.

Ret instruction pops return

address from stack and jumps

back

5 Florian Kargl — IAIK – Graz University of Technology

Page 14: Systems Programming - A6 - IAIK

Function Arguments www.tugraz.at

Easy enough, but what about function arguments and return values?

Caller

int main()

{

char arg1 = 5;

char arg2 = 7;

int retval = foo(arg1 , arg2);

}

Callee

int foo(char a, char b)

{

return a > b;

}

How does this...

6 Florian Kargl — IAIK – Graz University of Technology

Page 15: Systems Programming - A6 - IAIK

Function Arguments www.tugraz.at

Easy enough, but what about function arguments and return values?

Caller

int main()

{

char arg1 = 5;

char arg2 = 7;

int retval = foo(arg1 , arg2);

}

Callee

int foo(char a, char b)

{

return a > b;

}

How does this...

...get here?

6 Florian Kargl — IAIK – Graz University of Technology

Page 16: Systems Programming - A6 - IAIK

Function Arguments www.tugraz.at

Easy enough, but what about function arguments and return values?

Caller

int main()

{

char arg1 = 5;

char arg2 = 7;

int retval = foo(arg1 , arg2);

}

Callee

int foo(char a, char b)

{

return a > b;

}

And this...

6 Florian Kargl — IAIK – Graz University of Technology

Page 17: Systems Programming - A6 - IAIK

Function Arguments www.tugraz.at

Easy enough, but what about function arguments and return values?

Caller

int main()

{

char arg1 = 5;

char arg2 = 7;

int retval = foo(arg1 , arg2);

}

Callee

int foo(char a, char b)

{

return a > b;

}

And this...

...back here?

6 Florian Kargl — IAIK – Graz University of Technology

Page 18: Systems Programming - A6 - IAIK

Function Arguments www.tugraz.at

Where do we put the function arguments?

• Registers

• Which ones?

• What if we don’t have enough registers?

• Memory (i.e. on the stack)

• In which order?

7 Florian Kargl — IAIK – Graz University of Technology

Page 19: Systems Programming - A6 - IAIK

Function Arguments www.tugraz.at

Where do we put the function arguments?

• Registers

• Which ones?

• What if we don’t have enough registers?

• Memory (i.e. on the stack)

• In which order?

7 Florian Kargl — IAIK – Graz University of Technology

Page 20: Systems Programming - A6 - IAIK

Function Arguments www.tugraz.at

Where do we put the function arguments?

• Registers

• Which ones?

• What if we don’t have enough registers?

• Memory (i.e. on the stack)

• In which order?

7 Florian Kargl — IAIK – Graz University of Technology

Page 21: Systems Programming - A6 - IAIK

Function Arguments www.tugraz.at

Where do we put the function arguments?

• Registers

• Which ones?

• What if we don’t have enough registers?

• Memory (i.e. on the stack)

• In which order?

7 Florian Kargl — IAIK – Graz University of Technology

Page 22: Systems Programming - A6 - IAIK

Function Arguments www.tugraz.at

Where do we put the function arguments?

• Registers

• Which ones?

• What if we don’t have enough registers?

• Memory (i.e. on the stack)

• In which order?

7 Florian Kargl — IAIK – Graz University of Technology

Page 23: Systems Programming - A6 - IAIK

Function Arguments www.tugraz.at

Where do we put the function arguments?

• Registers

• Which ones?

• What if we don’t have enough registers?

• Memory (i.e. on the stack)

• In which order?

7 Florian Kargl — IAIK – Graz University of Technology

Page 24: Systems Programming - A6 - IAIK

Calling Conventions www.tugraz.at

A calling convention defines the interaction between functions on the level of

CPU-instructions

• Function parameters

• Return values

• Registers that need to be saved/restored across function calls

8 Florian Kargl — IAIK – Graz University of Technology

Page 25: Systems Programming - A6 - IAIK

Calling Conventions www.tugraz.at

Calling conventions are not only relevant within a single binary. All interfaces between

binary modules need to conform to a common interface to be compatible.

• Object files that are linked together at compile time

• Dynamically loaded libraries (e.g. libc)

⇒ Defined as part of an ABI (Application Binary Interface)

• A complete ABI also defines the executable format (e.g. ELF), instruction set, ...

9 Florian Kargl — IAIK – Graz University of Technology

Page 26: Systems Programming - A6 - IAIK

Calling Conventions www.tugraz.at

Calling conventions are not only relevant within a single binary. All interfaces between

binary modules need to conform to a common interface to be compatible.

• Object files that are linked together at compile time

• Dynamically loaded libraries (e.g. libc)

⇒ Defined as part of an ABI (Application Binary Interface)

• A complete ABI also defines the executable format (e.g. ELF), instruction set, ...

9 Florian Kargl — IAIK – Graz University of Technology

Page 27: Systems Programming - A6 - IAIK

Calling Conventions www.tugraz.at

Calling conventions are not only relevant within a single binary. All interfaces between

binary modules need to conform to a common interface to be compatible.

• Object files that are linked together at compile time

• Dynamically loaded libraries (e.g. libc)

⇒ Defined as part of an ABI (Application Binary Interface)

• A complete ABI also defines the executable format (e.g. ELF), instruction set, ...

9 Florian Kargl — IAIK – Graz University of Technology

Page 28: Systems Programming - A6 - IAIK

Calling Conventions www.tugraz.at

The used ABI/calling convention depends on

• CPU architecture

• Operating system

• Compiler

Mostly standardized

10 Florian Kargl — IAIK – Graz University of Technology

Page 29: Systems Programming - A6 - IAIK

Calling Conventions www.tugraz.at

The used ABI/calling convention depends on

• CPU architecture

• Operating system

• Compiler

Mostly standardized

10 Florian Kargl — IAIK – Graz University of Technology

Page 30: Systems Programming - A6 - IAIK

Calling Conventions www.tugraz.at

Commonly used calling conventions

Linux Windows

i386 cdecl cdecl, stdcall, fastcall, ... *

x86 64 System V amd64 ABI Microsoft x64

System calls usually use a different calling convention than the rest of the userspace

* 32-bit Windows is a bit of a mess

11 Florian Kargl — IAIK – Graz University of Technology

Page 31: Systems Programming - A6 - IAIK

Calling Conventions www.tugraz.at

Calling conventions relevant for the assignment

Linux Windows

i386 cdecl cdecl, stdcall, fastcall, ...

x86 64 System V amd64 ABI Microsoft x64

Main difference: Function arguments on stack vs. in registers

12 Florian Kargl — IAIK – Graz University of Technology

Page 32: Systems Programming - A6 - IAIK

GCC Inline Assembly www.tugraz.at

In this assignment you will need to write (inline) assembly.

No C code allowed!

13 Florian Kargl — IAIK – Graz University of Technology

Page 33: Systems Programming - A6 - IAIK

GCC Inline Assembly www.tugraz.at

GCC allows you to write assembly code inside C functions

GCC Inline Assembly

int foobar(uint64_t* result) {

uint64_t a = 3;

uint64_t b = 4;

asm("movq %[op1], %%rax\n"

"addq %[op2], %%rax\n"

"movq %%rax , %[res]\n"

:[res]"=m"(* result) // output (memory location , not value)

:[op1]"m"(a), // input (op1 in memory)

[op2]"r"(b) // (op2 in register)

:"rax", "cc"); // clobbers the rax register and status flags

("m" output constraint -> no need to explicity list "memory ")

}

14 Florian Kargl — IAIK – Graz University of Technology

Page 34: Systems Programming - A6 - IAIK

Your Tasks

Page 35: Systems Programming - A6 - IAIK

Tasks www.tugraz.at

• A - Implement the caller side of a function call usinggcc inline assembly

• cdecl (32-bit)

• System V amd64 ABI (64-bit)

• B - Implement a function in assembly for differentcalling conventions

• cdecl (32-bit)

• System V amd64 ABI (64-bit)

• C - Call Linux system calls using gcc inline assembly

• int 0x80 (32-bit)

• syscall (64-bit)

foo(); int foo(){ ... }

16 Florian Kargl — IAIK – Graz University of Technology

Page 36: Systems Programming - A6 - IAIK

Tasks www.tugraz.at

• A - Implement the caller side of a function call usinggcc inline assembly

• cdecl (32-bit)

• System V amd64 ABI (64-bit)

• B - Implement a function in assembly for differentcalling conventions

• cdecl (32-bit)

• System V amd64 ABI (64-bit)

• C - Call Linux system calls using gcc inline assembly

• int 0x80 (32-bit)

• syscall (64-bit)

foo(); int foo(){ ... }

16 Florian Kargl — IAIK – Graz University of Technology

Page 37: Systems Programming - A6 - IAIK

Tasks www.tugraz.at

• A - Implement the caller side of a function call usinggcc inline assembly

• cdecl (32-bit)

• System V amd64 ABI (64-bit)

• B - Implement a function in assembly for differentcalling conventions

• cdecl (32-bit)

• System V amd64 ABI (64-bit)

• C - Call Linux system calls using gcc inline assembly

• int 0x80 (32-bit)

• syscall (64-bit)

ioctl syscall

Linux kernel module

16 Florian Kargl — IAIK – Graz University of Technology

Page 38: Systems Programming - A6 - IAIK

Questions?