Upload
others
View
1
Download
0
Embed Size (px)
Citation preview
Praktikum ASP (IN0035)Einführungsvorlesung
Alexis Engelke
Lehrstuhl für Rechnerarchitektur und Parallele SystemeFakultät für InformatikTechnische Universität München
16. Oktober 2019
2
Alexis Engelke 2019
Wie funktioniert die Programmierungvon Prozessoren auf niedriger Ebene?
I Welche Schnittstelle zu (Anwendungs-)Softwarehat ein Prozessor?
I Wie kann man diese möglichst effizient nutzen?
Motivation Organisation Grundlagen AArch64 Toolchain
3
Alexis Engelke 2019
Ziele des Praktikums
I Vermittlung von Low-level ProgrammierungI 64-Bit ARM-Architektur (AArch64) auf Raspberry Pi 3
I Verständnis der Programm-Ausführung auf HardwareI Effizienz: Was kann Hardware gut?I Gezielte Code-OptimierungI Leistungsanalyse, BenchmarkingI Sichere Programmierung
Motivation Organisation Grundlagen AArch64 Toolchain
4
Alexis Engelke 2019
Cluster von 40 Raspberry Pi 3
Motivation Organisation Grundlagen AArch64 Toolchain
6
Alexis Engelke 2019
Wofür ist das gut?
I Gezielte Anpassung von Programmen, ermöglichen. . .I dass ein Compiler schnellen Code generieren kannI Optimierungen, die ein Compiler nicht (gut) kann
I Games-Bezug: Höhere Performance
I DebuggingI Bei komplexen Bugs, Compiler-Bugs, CPU-Bugs, etc.
I Sichere ProgrammierungI Schwachstellen im Code vermeiden
Motivation Organisation Grundlagen AArch64 Toolchain
7
Alexis Engelke 2019
Organisation des Praktikums
I Modul IN0035, 5 ECTSI Keine Klausur, Bewertung über ProjektarbeitI Website: https://www.caps.in.tum.de/asp
I Ansprechpartner:1. Ihr Tutor ist der erste Ansprechpartner2. Praktikumsleitung (Alexis Engelke)
I Bei Problemen mit Rechnerräumen: RBGI Bei Problemen mit Praktikumsinfrastruktur: Tutor
Motivation Organisation Grundlagen AArch64 Toolchain
https://www.caps.in.tum.de/asp
8
Alexis Engelke 2019
Ablauf
I Einführungsvorlesung: 16.10.2019
I Phase 1: Wöchentliche PraktikumsstundenI Start ab 21.10.2019, jeder sollte angemeldet seinI Anwesenheit ratsam
I Phase 2: ProjektarbeitI Zuteilung in Dreiergruppen (Anfang Dezember)I Bearbeitung einer individuellen ProjektaufgabeI Ab Januar Sprechstunde statt Tutorien in 01.08.011
Während der Projektphase werdenkeine Fragen zum Grundstoff beantwortet!
Motivation Organisation Grundlagen AArch64 Toolchain
9
Alexis Engelke 2019
ZeitplanI Anmeldung zu Projektphase bis 29.11.2019I Vergabe Projektaufgaben 04.12.2019I Kurzvortrag 09.12. – 13.12.2019
I Vorstellung der Aufgabe in einem TutoriumI Anwesenheitspflicht
I Ende Tutorien 20.12.2019I Abgabefrist 29.01.2020I Abschlussvorträge 02.03. – 06.03.2020
I Anwesenheitspflicht (für eigenen Vortrag)I Bei unbegründeter Abwesenheit Bewertung mit 5.0
I Weitere Details folgen per E-Mail und auf WebsiteMotivation Organisation Grundlagen AArch64 Toolchain
10
Alexis Engelke 2019
Bewertung Projektaufgabe
I Grundlage: PraktikumsordnungI Unbedingt lesen!I Neues Bewertungsschema ab WiSe 19/20!I Enthält Details zu Durchführung und Bewertung
I Bewertung von:1. Implementierung (20 Pkt.)2. Ausarbeitung (20 Pkt.)3. Abschlussvortrag (20 Pkt.)
I Endnote 5.0 möglichI Plagiate: Abgabe von nicht selbst-geschriebenem Code
führt zu Konsequenzen: Note 5.0 bis Exmatrikulation
Motivation Organisation Grundlagen AArch64 Toolchain
11
Alexis Engelke 2019
Organisatorisches: Verschiedenes
I Besuch von Veranstaltung IN0034 empfohlenI Erfahrung mit Linux-Konsole und C-Programmierung
unabdingbare Voraussetzung!
I Hardware: Zugriff zu Raspberry Pi 3 vom LehrstuhlI Fernzugriff von zuhause möglich
I Literatur: ARM-Dokumentation, LV-Materialien
Motivation Organisation Grundlagen AArch64 Toolchain
12
Alexis Engelke 2019
ARM
I ARM Holdings (früher: Advanced RISC Machines)I Erstellt Prozessor-Designs für mobile/embedded Systeme
I Keine Chipherstellung!I Verkauft Lizenzen der Architektur,
u.a. an Apple, Nvidia, Broadcom, Intel, Cavium, . . .
I Design kann leicht geändert/erweitert werdenI Z.B. für GPUs, Netzwerkcontroller, . . .
I Geringer Stromverbrauch, Preis (?)
Motivation Organisation Grundlagen AArch64 Toolchain
13
Alexis Engelke 2019
Instruction Set Architecture
I Schnittstelle zwischen Programm und ProzessorI Hardware-Realisierung für Programmierer nicht wichtig
I Welche Modi werden unterstützt?I Bei ARM: AArch32, AArch64I Aber auch: Betriebssystem-Modi
I Welche Datentypen?I Byte, Word (32-Bit bei ARM), Float, Double, . . .
I Welche Instruktionen, Register?I Wie wird Maschinencode codiert?
Motivation Organisation Grundlagen AArch64 Toolchain
15
Alexis Engelke 2019
Überblick AArch64Core
Speicher
x0x1x2x3x4. . .x28x29x30
Register (64 Bit)
xzr
sp
pc
nzcv
Steuerungs-/Berechnungslogik
Code Data Stack
Low Addr → High Addrpc
sp
I RegisterI 31 General-PurposeI Zero Register xzrI Program Counter pc
I Flags Register nzcvI Stack Pointer sp
I SpeicherI Byte-weise adressierbarI Enthält Code und Daten
Motivation Organisation Grundlagen AArch64 Toolchain
16
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0
func:mov x0, 123ret
long func() {return 123;
}
Motivation Organisation Grundlagen AArch64 Toolchain
17
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0I Argumente in x0–x7
func:add x0, x0, 5ret
long func(long x0) {return x0 + 5;
}
Motivation Organisation Grundlagen AArch64 Toolchain
18
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0I Argumente in x0–x7
func:add x0, x0, x1ret
long func(long x0, long x1) {return x0 + x1;
}
Motivation Organisation Grundlagen AArch64 Toolchain
19
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0I Argumente in x0–x7
func:sub x0, xzr, x0ret
long func(long x0) {return -x0;
}
Motivation Organisation Grundlagen AArch64 Toolchain
20
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0I Argumente in x0–x7
func:mul x1, x0, x0add x0, x1, x0ret
long func(long x0) {return x0 * x0 + x0;
}
Motivation Organisation Grundlagen AArch64 Toolchain
21
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0I Argumente in x0–x7
func:add x1, x0, 1mul x0, x0, x1mov x1, 2sdiv x0, x0, x1ret
long func(long x0) {return x0 * (x0 + 1) / 2;
}
Motivation Organisation Grundlagen AArch64 Toolchain
22
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0I Argumente in x0–x7
I Label enden mit :I Sprünge mit b.cond
I eq equalle less-or-equal
gt greater-than
func:cmp x0, 0b.eq .Lequalsub x0, x0, 1
.Lequal:ret
long func(long x0) {if (x0 != 0) x0 -= 1;return x0;
}
Motivation Organisation Grundlagen AArch64 Toolchain
25
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0I Argumente in x0–x7
I Label enden mit :I Sprünge mit b.cond
I eq equalle less-or-equalgt greater-than
abs:cmp x0, 0b.ge .Lpositivesub x0, xzr, x0
.Lpositive:ret
long abs(long x0) {if (x0 < 0) x0 = -x0;return x0;
}
Motivation Organisation Grundlagen AArch64 Toolchain
26
Alexis Engelke 2019
Arten von Instruktionen
I Arithmetisch-logische InstruktionenI add, sub, eor, mov, . . .
I Kontrollfluss-InstruktionenI b, b.cond, ret, . . .
I Speicherzugriffs-InstruktionenI ldr, str, (. . . )
I I/O-InstruktionenI Systeminstruktionen
Motivation Organisation Grundlagen AArch64 Toolchain
28
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0I Argumente in x0–x7
I Laden: ldrI Schreiben: str
func:ldr x0, [x0]ret
long func(long x0[]) {return x0[0];
}
Motivation Organisation Grundlagen AArch64 Toolchain
29
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0I Argumente in x0–x7
I Laden: ldrI Schreiben: str
func:ldr x1, [x0]add x1, x1, 1str x1, [x0]ret
void func(long x0[]) {x0[0] += 1;
}
Motivation Organisation Grundlagen AArch64 Toolchain
30
Alexis Engelke 2019
AArch64 Assembler
I Rückgabewert in x0I Argumente in x0–x7
I Laden: ldrI Schreiben: str
func:ldr x1, [x0]ldr x2, [x0, 8]add x1, x1, x2str x1, [x0]ret
void func(long x0[]) {x0[0] += x0[1];
}
Motivation Organisation Grundlagen AArch64 Toolchain
31
Alexis Engelke 2019
Überblick AArch64Core
Speicher
x0x1x2x3x4. . .x28x29x30
Register (64 Bit)
xzr
sp
pc
nzcv
Steuerungs-/Berechnungslogik
Code Data Stack
Low Addr → High Addrpc sp
I RegisterI 31 General-PurposeI Zero Register xzrI Program Counter pcI Flags Register nzcvI Stack Pointer sp
I SpeicherI Byte-weise adressierbarI Enthält Code und Daten
Motivation Organisation Grundlagen AArch64 Toolchain
32
Alexis Engelke 2019
Stack
I Erlaubt Funktionen, Daten temporär zu speichernI Lokale VariablenI Register, die von aufgerufener Funktion benutzt werden
I Wächst historisch von hoher Adresse zur niedrigenI Größe begrenzt, keine großen Datenmengen alloziieren
I Stack-Pointer-Register spI Zeigt immer auf unteres Ende vom StackI Muss immer 16-Byte ausgerichtet sein
Motivation Organisation Grundlagen AArch64 Toolchain
33
Alexis Engelke 2019
Einordnung
Assembler
Maschinencode
Interpreter/VM(mit JIT-Compiler)
Compiler
Java
Compiler
C, C++, Swift,Rust, Go, . . .
Python, R,JavaScript, . . .
Byte Code
Motivation Organisation Grundlagen AArch64 Toolchain
34
Alexis Engelke 2019
Übersicht: C-Compiler
gcc -o fileA.i -E fileA.c
fileA.c fileA.iPreprocessor
cpp
fileA.sC-Compiler
cc1fileA.o
Assembler
as execLinker
ld
fileB.S fileB.sPreprocessor
cppfileB.o
Assembler
as
objdump -d
Disassembler
???Decompiler
Motivation Organisation Grundlagen AArch64 Toolchain
34
Alexis Engelke 2019
Übersicht: C-Compiler
gcc -o fileA.s -S fileA.c
fileA.c fileA.iPreprocessor
cppfileA.s
C-Compiler
cc1
fileA.oAssembler
as execLinker
ld
fileB.S fileB.sPreprocessor
cppfileB.o
Assembler
as
objdump -d
Disassembler
???Decompiler
Motivation Organisation Grundlagen AArch64 Toolchain
35
Alexis Engelke 2019
OptimierungsoptionenI -O0 – no optimizationI -O1 – “optimize”
I Better register allocation, dead code elimination, . . .I -O2 – “optimize even more”
I More aggressive CSE, remove redundant instructions, . . .I -O3 – “optimize yet more”
I Aggressive inlining, vectorization, . . .I -Os – “optimize for size”I -Og – “optimize debugging experience”I -Ofast – “disregard strict standards compliance.”
I Floating-point optimizationsI -march=native (in addition) – architecture tuning
Motivation Organisation Grundlagen AArch64 Toolchain
36
Alexis Engelke 2019
Übersicht: C-Compiler
gcc -o fileA.o -c fileA.c
fileA.c fileA.iPreprocessor
cppfileA.s
C-Compiler
cc1fileA.o
Assembler
as
execLinker
ld
fileB.S fileB.sPreprocessor
cppfileB.o
Assembler
as
objdump -d
Disassembler
???Decompiler
Motivation Organisation Grundlagen AArch64 Toolchain
36
Alexis Engelke 2019
Übersicht: C-Compiler
gcc -o exec fileA.c
fileA.c fileA.iPreprocessor
cppfileA.s
C-Compiler
cc1fileA.o
Assembler
as execLinker
ld
fileB.S fileB.sPreprocessor
cppfileB.o
Assembler
as
objdump -d
Disassembler
???Decompiler
Motivation Organisation Grundlagen AArch64 Toolchain
36
Alexis Engelke 2019
Übersicht: C-Compiler
gcc -o exec fileA.c fileB.S
fileA.c fileA.iPreprocessor
cppfileA.s
C-Compiler
cc1fileA.o
Assembler
as execLinker
ld
fileB.S fileB.sPreprocessor
cppfileB.o
Assembler
as
objdump -d
Disassembler
???Decompiler
Motivation Organisation Grundlagen AArch64 Toolchain
37
Alexis Engelke 2019
Reverse Engineering
I Rekonstruktion von Programm aus BinaryI Motivation
I Finden/Beheben von SchwachstellenI Malware-AnalyseI Spionage (z.B. von Konkurrenten) – (i.d.R.) illegalI Cracks – (i.d.R.) illegal
I Rev.Eng. von fremder Software rechtlich schwierigI Vorher um Erlaubnis fragen / Anwalt konsolutieren
Motivation Organisation Grundlagen AArch64 Toolchain
38
Alexis Engelke 2019
Reverse Engineering
objdump -xd exec
fileA.c fileA.iPreprocessor
cppfileA.s
C-Compiler
cc1fileA.o
Assembler
as execLinker
ld
fileB.S fileB.sPreprocessor
cppfileB.o
Assembler
as
objdump -d
Disassembler
???Decompiler
Motivation Organisation Grundlagen AArch64 Toolchain
39
Alexis Engelke 2019
Disassembly
I Kommando: objdump
I Menschenlesbare Form von MaschinencodeI Gegenstück zu as
I Aber: Nicht 1-zu-1 wieder kompilierbar
I Einfaches Problem: Decodierung wie im Manual
Motivation Organisation Grundlagen AArch64 Toolchain
40
Alexis Engelke 2019
Disassembly
0000000000000000 :0: 2a0003e2 mov w2, w04: 7100041f cmp w0, #0x18: 5400012d b.le 2c c: 52800021 mov w1, #0x1
10: 2a0103e0 mov w0, w114: d503201f nop18: 1b017c00 mul w0, w0, w11c: 11000421 add w1, w1, #0x120: 6b01005f cmp w2, w124: 54ffffa1 b.ne 18 28: d65f03c0 ret2c: 52800020 mov w0, #0x130: d65f03c0 ret
Motivation Organisation Grundlagen AArch64 Toolchain
41
Alexis Engelke 2019
Reverse Engineering
fileA.c fileA.iPreprocessor
cppfileA.s
C-Compiler
cc1fileA.o
Assembler
as execLinker
ld
fileB.S fileB.sPreprocessor
cppfileB.o
Assembler
as
objdump -d
Disassembler
???Decompiler
Motivation Organisation Grundlagen AArch64 Toolchain
42
Alexis Engelke 2019
Dekompilieren
I Sehr schwieriges Problem!I Behandlung von speziellen InstruktionssequenzenI KontrollflussrekonstruktionI Erkennung von Datentypen und StrukturenI Variablen als solche erkennenI . . .
Motivation Organisation Grundlagen AArch64 Toolchain
43
Alexis Engelke 2019
Zusammenfassung
I Einführung in AArch64 ArchitekturI Häufig verwendete RegisterI Grundlegende arithmetische InstruktionenI Speicherzugriff
I Speicherlayout: Stack für verschachtelte FunktionsaufrufeI Toolchain: Compiler, Assembler, Linker, Disassembler
Motivation Organisation Grundlagen AArch64 Toolchain
Häufige Fehler: Ausarbeitung
1. Beschreibung der Implementierung zu detailliert2. Keine Beispiele, Korrektheit nicht gezeigt3. Keine Darstellung von Ergebnissen
I Formfehler: Starke PunktabzügeI Mehrere Ausarbeitungen, keine TEX-Datei, etc.
Häufige Fehler: Implementierung
1. Segmentation FaultI Z.B. Buffer Overflow, malloc nicht geprüft
2. Calling Convention nicht eingehalten3. Keine Korrektheitstests
I Abgabe kompiliert nicht auf Referenzplattform: Endnote5.0
I Compiler-generierter Code: Plagiat!
Häufige Fehler: Vortrag
1. Keine angemessene Wahl der InhalteI Z.B. viele Code-Listings, Programmaufruf
2. Kein Blickkontakt zum Publikum, Ablesen3. Schwer lesbare Folien, keine Foliennummern
I Formfehler: Starke PunktabzügeI Folien nicht abgegeben, nicht als PDF, etc.
Vielen Dank!
MotivationOrganisationGrundlagen AArch64Toolchain