Systems Programming - A6 - IAIK
Transcript of Systems Programming - A6 - IAIK
![Page 1: Systems Programming - A6 - IAIK](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/1.jpg)
Systems Programming
A6
Florian Kargl
November 22, 2019
IAIK – Graz University of Technology
![Page 2: Systems Programming - A6 - IAIK](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/2.jpg)
A6 - Inline Assembly and Calling Conventions
1 Florian Kargl — IAIK – Graz University of Technology
![Page 3: Systems Programming - A6 - IAIK](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/3.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/4.jpg)
A6 - Inline Assembly and Calling
Conventions
![Page 5: Systems Programming - A6 - IAIK](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/5.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/6.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/7.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/8.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/9.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/10.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/11.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/12.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/13.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/14.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/15.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/16.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/17.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/18.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/19.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/20.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/21.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/22.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/23.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/24.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/25.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/26.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/27.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/28.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/29.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/30.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/31.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/32.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/33.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/34.jpg)
Your Tasks
![Page 35: Systems Programming - A6 - IAIK](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/35.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/36.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/37.jpg)
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](https://reader035.fdocuments.us/reader035/viewer/2022081615/62ca3fbc8945081da7510707/html5/thumbnails/38.jpg)
Questions?