Skip to content

Commit e3c483b

Browse files
x86_64: move the pushing and popping common structs into a macro
* push/pop_preserved * push/pop_scratch * update the build system to recognise `.inc` assembly include files :^) Signed-off-by: Andy-Python-Programmer <[email protected]>
1 parent a395a81 commit e3c483b

File tree

5 files changed

+117
-101
lines changed

5 files changed

+117
-101
lines changed

src/aero_kernel/build.rs

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ use std::fs::DirEntry;
2525
use std::path::Path;
2626
use std::process::Command;
2727

28+
//this is all magic, yes dont ever let anyone see this shit
29+
2830
/// Helper function of walking the provided `dir`, only visiting files and calling
2931
/// `cb` on each file.
30-
fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry)) -> std::io::Result<()> {
32+
fn visit_dirs(dir: &Path, cb: &mut dyn FnMut(&DirEntry)) -> std::io::Result<()> {
3133
if dir.is_dir() {
3234
for entry in fs::read_dir(dir)? {
3335
let entry = entry?;
@@ -45,9 +47,11 @@ fn visit_dirs(dir: &Path, cb: &dyn Fn(&DirEntry)) -> std::io::Result<()> {
4547
}
4648

4749
fn main() -> Result<(), Box<dyn Error>> {
50+
let mut inc_files = vec![];
51+
4852
// Assemble all of the assembly real files first as they will be included in the
4953
// source files using `incbin`.
50-
visit_dirs(Path::new("src"), &|entry| {
54+
visit_dirs(Path::new("src"), &mut |entry| {
5155
let path = entry.path();
5256

5357
match path.extension() {
@@ -68,26 +72,52 @@ fn main() -> Result<(), Box<dyn Error>> {
6872
assert!(success);
6973
}
7074

75+
Some(ext) if ext.eq(&OsString::from("inc")) => {
76+
let path = path
77+
.to_str()
78+
.expect("invalud UTF-8 for file path (skill issue)");
79+
inc_files.push(path.to_string())
80+
}
81+
7182
_ => (),
7283
}
7384
})?;
7485

86+
// more magic
87+
inc_files = inc_files
88+
.iter()
89+
.map(|e| {
90+
let e = e.split("/").collect::<Vec<_>>();
91+
e[..e.len() - 1].join("/").to_string()
92+
})
93+
.collect::<Vec<_>>();
94+
7595
// Now that we have assembled all of the real files, we can go ahead and assemble the source
7696
// files.
77-
visit_dirs(Path::new("src"), &|entry: &DirEntry| {
97+
visit_dirs(Path::new("src"), &mut |entry: &DirEntry| {
7898
let path = entry.path();
7999

80100
match path.extension() {
81101
Some(ext) if ext.eq(&OsString::from("asm")) => {
82102
let object_os = path.file_name().expect("Failed to get file name");
83103
let object_file = object_os.to_str().expect("Invalid UTF-8 for file name");
84104

85-
nasm_rs::Build::new()
105+
let mut build = nasm_rs::Build::new();
106+
107+
build
86108
.file(&path)
87109
.flag("-felf64")
88-
.target("x86_64-unknown-none")
110+
.target("x86_64-unknown-none");
111+
112+
println!("{:?}", inc_files);
113+
114+
for include in &inc_files {
115+
build.include(include);
116+
}
117+
118+
build
89119
.compile(object_file)
90-
.expect("Failed to compile assembly source file");
120+
.expect("failed to compile assembly: skill issue");
91121

92122
// Link it as a static library.
93123
println!("cargo:rustc-link-lib=static={}", object_file);

src/aero_kernel/src/arch/x86_64/interrupts/handlers.asm

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
bits 64
1919

20+
%include "registers.inc"
21+
2022
global interrupt_handlers
2123

2224
extern generic_interrupt_handler
@@ -42,23 +44,9 @@ interrupt_handler_%1:
4244

4345
xchg [rsp], rax
4446

45-
; push the scratch registers
46-
push rcx
47-
push rdx
48-
push rdi
49-
push rsi
50-
push r8
51-
push r9
52-
push r10
53-
push r11
54-
55-
; push the preserved registers
56-
push rbx
57-
push rbp
58-
push r12
59-
push r13
60-
push r14
61-
push r15
47+
; note: RAX is now on the top of the stack.
48+
push_scratch
49+
push_preserved
6250

6351
push rax
6452

@@ -71,24 +59,8 @@ interrupt_handler_%1:
7159
; pop the error code
7260
add rsp, 8
7361

74-
; pop the preserved registers
75-
pop r15
76-
pop r14
77-
pop r13
78-
pop r12
79-
pop rbp
80-
pop rbx
81-
82-
; pop the scratch registers
83-
pop r11
84-
pop r10
85-
pop r9
86-
pop r8
87-
pop rsi
88-
pop rdi
89-
pop rdx
90-
pop rcx
91-
pop rax
62+
pop_preserved
63+
pop_scratch
9264

9365
; `swapgs` if the interrupt has originated from ring 3 since currently
9466
; the GS base points to the kernel per-cpu data.
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
; pops the preserved registers (the stack must have the preserved registers pushed on
2+
; top and matches the layout of the `PreservedRegisters` structure)
3+
%macro pop_preserved 0
4+
pop r15
5+
pop r14
6+
pop r13
7+
pop r12
8+
pop rbp
9+
pop rbx
10+
%endmacro
11+
12+
; pops the scratch registers (the stack must have the scratch registers pushed on
13+
; top and matches the layout of the `ScratchRegisters` structure)
14+
%macro pop_scratch 0
15+
pop r11
16+
pop r10
17+
pop r9
18+
pop r8
19+
pop rsi
20+
pop rdi
21+
pop rdx
22+
pop rcx
23+
pop rax
24+
%endmacro
25+
26+
; pushes the scratch registers on the stack. The layout matches the layout of
27+
; the `ScratchRegisters` structure though note that it does not push the RAX
28+
; register. The caller is responsible for pushing RAX on the top of the stack.
29+
%macro push_scratch 0
30+
push rcx
31+
push rdx
32+
push rdi
33+
push rsi
34+
push r8
35+
push r9
36+
push r10
37+
push r11
38+
%endmacro
39+
40+
; pushes the preserved registers on the stack. The layout matches the layout of
41+
; the `PreservedRegisters` structure.
42+
%macro push_preserved 0
43+
push rbx
44+
push rbp
45+
push r12
46+
push r13
47+
push r14
48+
push r15
49+
%endmacro

src/aero_kernel/src/arch/x86_64/syscall_handler.asm

Lines changed: 8 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
; You should have received a copy of the GNU General Public License
1616
; along with Aero. If not, see <https://www.gnu.org/licenses/>.
1717

18+
bits 64
19+
20+
%include "registers.inc"
21+
1822
extern x86_64_do_syscall
1923
global x86_64_syscall_handler
2024

@@ -60,24 +64,8 @@ x86_64_syscall_handler:
6064
push rcx ; push userspace return pointer
6165

6266
push rax
63-
64-
; push the scratch registers
65-
push rcx
66-
push rdx
67-
push rdi
68-
push rsi
69-
push r8
70-
push r9
71-
push r10
72-
push r11
73-
74-
; push the preserved registers
75-
push rbx
76-
push rbp
77-
push r12
78-
push r13
79-
push r14
80-
push r15
67+
push_scratch
68+
push_preserved
8169

8270
; push a "fake" error code to match with the layout of the
8371
; `InterruptErrorStack` structure.
@@ -92,24 +80,8 @@ x86_64_syscall_handler:
9280
; pop the "fake" error code
9381
add rsp, 8
9482

95-
; pop the preserved registers
96-
pop r15
97-
pop r14
98-
pop r13
99-
pop r12
100-
pop rbp
101-
pop rbx
102-
103-
; pop the scratch registers
104-
pop r11
105-
pop r10
106-
pop r9
107-
pop r8
108-
pop rsi
109-
pop rdi
110-
pop rdx
111-
pop rcx
112-
pop rax
83+
pop_preserved
84+
pop_scratch
11385

11486
; make the sysret frame
11587
pop rcx

src/aero_kernel/src/arch/x86_64/task.asm

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
; You should have received a copy of the GNU General Public License
1616
; along with Aero. If not, see <https://www.gnu.org/licenses/>.
1717

18+
bits 64
19+
20+
%include "registers.inc"
21+
1822
global jump_userland_exec
1923
global task_spinup
2024
global iretq_init
@@ -34,38 +38,27 @@ jump_userland_exec:
3438
swapgs
3539
o64 sysret
3640

37-
fork_init:
38-
cli
39-
swapgs
40-
jmp generic_iretq_init
41-
4241
iretq_init:
4342
cli
44-
jmp generic_iretq_init
4543

46-
generic_iretq_init:
4744
; pop the error code
4845
add rsp, 8
4946

50-
; pop the preserved registers
51-
pop r15
52-
pop r14
53-
pop r13
54-
pop r12
55-
pop rbp
56-
pop rbx
47+
pop_preserved
48+
pop_scratch
5749

58-
; pop the scratch registers
59-
pop r11
60-
pop r10
61-
pop r9
62-
pop r8
63-
pop rsi
64-
pop rdi
65-
pop rdx
66-
pop rcx
67-
pop rax
50+
iretq
51+
52+
fork_init:
53+
cli
6854

55+
; pop the error code
56+
add rsp, 8
57+
58+
pop_preserved
59+
pop_scratch
60+
61+
swapgs
6962
iretq
7063

7164
; extern "C" fn task_spinup(prev: &mut Context, next: &mut Context)

0 commit comments

Comments
 (0)