Skip to content

Commit dba4dac

Browse files
committed
cilibc: Add read syscall, extend headers
1 parent 090ec8e commit dba4dac

File tree

16 files changed

+235
-21
lines changed

16 files changed

+235
-21
lines changed

Justfile

+14-9
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ virtio-blk := "virtio-blk-pci,drive=dsk0,disable-legacy=on,disable-modern=off"
1616

1717
# qemu-args := " -serial stdio" + " -m " + mem + " -drive " + drive + " -no-reboot -d cpu_reset -s" + " -netdev " + dev + " -object " + pcap + " -device " + virtio-blk
1818
#
19-
qemu-args := " -serial stdio" + " -m " + mem + " -drive " + drive + " -no-reboot -d cpu_reset -s" + " -device " + virtio-net + " -netdev " + dev + " -object " + pcap + " -device " + virtio-blk
19+
qemu-args := " -serial stdio" + " -m " + mem + " -drive " + drive + " -no-reboot -d cpu_reset -s" + " -device " + virtio-net + " -netdev " + dev + " -object " + pcap + " -device " + virtio-blk + " 2>/dev/null"
2020

2121
limine := "extern/limine/build/bin"
2222

@@ -41,25 +41,30 @@ build init release:
4141
just build {{init}} circinus
4242
popd
4343
44-
just build_kern {{init}} {{release}}
44+
just build_kern {{init}} {{release}} false
4545
46-
build_kern init release:
46+
build_kern init release silent:
4747
#!/usr/bin/env bash
4848
set -e
4949
export INIT_FILE={{init}}
50-
if { [ release != "debug" ] && [ release != "release" ] ;} then \
50+
if { [ {{release}} != "debug" ] && [ {{release}} != "release" ] ;} then \
5151
echo Unknown build mode \"{{release}}\";\
5252
exit 1; \
5353
fi;
5454
55-
cargo -Z build-std=core,alloc -Z build-std-features=compiler-builtins-mem build --target kernel/arch/x64/x64.json {{ if release == "debug" { "" } else { "--release" } }}
55+
if [ {{silent}} != "false" ]; then \
56+
cargo -Z build-std=core,alloc -Z build-std-features=compiler-builtins-mem build --target kernel/arch/x64/x64.json {{ if release == "debug" { "" } else { "--release" } }} 2>/dev/null; \
57+
else \
58+
cargo -Z build-std=core,alloc -Z build-std-features=compiler-builtins-mem build --target kernel/arch/x64/x64.json {{ if release == "debug" { "" } else { "--release" } }};
59+
fi;
60+
5661
cp target/{{target}}/{{release}}/kernel build/kernel.elf
5762
5863
nm build/kernel.elf | rustfilt | awk '{ $2=""; print $0 }' > build/kernel.sym
5964
python3 ./embed-symbol-table.py build/kernel.sym build/kernel.elf
6065
6166
image name:
62-
#!/usr/bin/env sh
67+
#!/usr/bin/env bash
6368
set -e
6469
path="build/{{name}}"
6570
@@ -119,8 +124,8 @@ run_gdb init="init" release="release": (build init release) (image img)
119124
kvm init="init" release="release": (build init release) (image img)
120125
sudo qemu-system-{{qemutarget}} -enable-kvm -cpu host {{qemu-args}}
121126
122-
run_file file release="release": (build_kern file release) (image img)
123-
qemu-system-{{qemutarget}} -cpu Haswell {{qemu-args}}
127+
run_file file silent="false" release="release": (build_kern file release silent) (image img)
128+
qemu-system-{{qemutarget}} -cpu Haswell {{qemu-args}}
124129
125130
qemu init="init" release="release": (image img)
126-
qemu-system-{{qemutarget}} -cpu Haswell {{qemu-args}}
131+
qemu-system-{{qemutarget}} -cpu Haswell {{qemu-args}}

_todo.md

+7
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,10 @@ command structure for kernel access, not reliant on fio
22
command buffer
33
fire and block
44
fire and forget
5+
6+
no idea what those are ^
7+
8+
message passing
9+
expose kernel font somehow (broadcast message and kernel replies?)
10+
11+

userland/cilibc/Makefile

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ sys = $(src_dir)/sys
55
sys64 = $(sys)/x86_64
66
string = $(src_dir)/string
77
stdio = $(src_dir)/stdio
8+
fcntl = $(src_dir)/fcntl
89
stdlib = $(src_dir)/stdlib
910

1011
crt_objects = $(sys64)/crt0.o $(sys64)/crti.o $(sys64)/crtn.o
@@ -13,12 +14,16 @@ other_objects = $(sys64)/syscall.o\
1314
$(sys)/exit.o\
1415
$(sys)/write.o\
1516
$(sys)/brk.o\
17+
$(sys)/read.o\
1618
$(string)/strlen.o\
1719
$(string)/memset.o\
20+
$(string)/strchr.o\
1821
$(stdio)/puts.o\
1922
$(stdio)/fops.o\
23+
$(fcntl)/fcntl.o\
2024
$(stdio)/printf.o\
2125
$(stdlib)/mem.o\
26+
$(stdlib)/stdlib.o\
2227
$(stdio)/__stdio.o\
2328
$(src_dir)/cilibc.o
2429
objects = $(crt_objects) $(other_objects)

userland/cilibc/include/fcntl.h

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#ifndef _CILIBC_FCNTL_H
2+
#define _CILIBC_FCNTL_H 1
3+
4+
#if defined(__cplusplus)
5+
extern "C" {
6+
#endif
7+
8+
// TODO: mode_t, off_t, pid_t
9+
10+
#define O_RDONLY 000000000
11+
#define O_WRONLY 000000001
12+
#define O_RDWR 000000002
13+
#define O_CREAT 000000100
14+
#define O_EXCL 000000200
15+
#define O_NOCTTY 000000400
16+
#define O_TRUNC 000001000
17+
#define O_APPEND 000002000
18+
#define O_NONBLOCK 000004000
19+
#define O_DIRECTORY 000200000
20+
#define O_CLOEXEC 002000000
21+
22+
int open(const char *, int, ...);
23+
int close(int);
24+
25+
#if defined(__cplusplus)
26+
} /* extern "C" */
27+
#endif
28+
29+
#endif /* _CILIBC_FCNTL_H */

userland/cilibc/include/stdint.h

+34-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,41 @@ extern "C" {
99
typedef unsigned long int uintptr_t;
1010
typedef long int intptr_t;
1111

12+
#ifndef __CILIBC__UINT8_TYPE__
13+
#define __CILIBC__UINT8_TYPE__ 1
14+
15+
#ifndef __UINT8_TYPE__
16+
#define __UINT8_TYPE__ unsigned char
17+
#endif
18+
19+
typedef __UINT8_TYPE__ uint8_t;
20+
21+
#endif
22+
23+
#ifndef __CILIBC__UINT32_TYPE__
24+
#define __CILIBC__UINT32_TYPE__ 1
25+
26+
#ifndef __UINT32_TYPE__
27+
#define __UINT32_TYPE__ unsigned int
28+
#endif
29+
30+
typedef __UINT32_TYPE__ uint32_t;
31+
32+
#endif
33+
34+
#ifndef __CILIBC__UINT64_TYPE__
35+
#define __CILIBC__UINT64_TYPE__ 1
36+
37+
#ifndef __UINT64_TYPE__
38+
#define __UINT64_TYPE__ unsigned long long
39+
#endif
40+
41+
typedef __UINT64_TYPE__ uint64_t;
42+
43+
#endif
44+
1245
#if defined(__cplusplus)
1346
} /* extern "C" */
1447
#endif
1548

16-
#endif /* _CILIBC_STDINT_H */
49+
#endif /* _CILIBC_STDINT_H */

userland/cilibc/include/stdlib.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,14 @@ void free(void *);
1717

1818
void *realloc(void*, size_t);
1919

20+
int abs(int);
21+
long labs(long);
22+
long long llabs(long long);
23+
2024
[[noreturn]] void exit(int);
2125

2226
#if defined(__cplusplus)
2327
} /* extern "C" */
2428
#endif
2529

26-
#endif /* _CILIBC_STDLIB_H */
30+
#endif /* _CILIBC_STDLIB_H */

userland/cilibc/include/sys/types.h

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#ifndef _CILIBC_SYS_TYPES_H
2+
#define _CILIBC_SYS_TYPES_H 1
3+
4+
#if defined(__cplusplus)
5+
#extern "C"
6+
#endif
7+
8+
#include <bits/sys/types.h>
9+
10+
#if defined(__cplusplus)
11+
} /* extern "C" */
12+
#endif
13+
14+
#endif /* _CILIBC_SYS_TYPES_H */

userland/cilibc/include/unistd.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ extern "C" {
88
#include <bits/sys/types.h>
99

1010
ssize_t write(int fd, const void* buf, size_t count);
11+
ssize_t read(int fd, void *, size_t);
12+
1113
void* brk(void* addr);
1214

1315
#define STDERR_FILENO 2
@@ -18,4 +20,4 @@ extern "C" {
1820
} /* extern "C" */
1921
#endif
2022

21-
#endif /* _CILIBC_UNISTD_H */
23+
#endif /* _CILIBC_UNISTD_H */

userland/cilibc/src/stdio/printf.c

+1-5
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,6 @@ int vprintf(const char* fmt, va_list arg) {
9191
char ch;
9292
char s;
9393

94-
char buf[2];
95-
9694
for (int i = 0; i < fmt_length; i++) {
9795
ch = fmt[i];
9896

@@ -116,9 +114,7 @@ int vprintf(const char* fmt, va_list arg) {
116114

117115
} else {
118116
written_len++;
119-
buf[0] = fmt[i];
120-
buf[1] = 0;
121-
puts(buf);
117+
putchar(ch);
122118
}
123119
}
124120

userland/cilibc/src/stdlib/stdlib.c

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdlib.h>
2+
3+
#define ABS(x) ({ \
4+
typeof(x) _x = (x); \
5+
_x < 0 ? -_x : _x; \
6+
})
7+
8+
int abs(int i) {
9+
return ABS(i);
10+
}
11+
12+
long labs(long l) {
13+
return ABS(l);
14+
}
15+
16+
long long llabs(long long l) {
17+
return ABS(l);
18+
}

userland/cilibc/src/sys/read.c

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#include <unistd.h>
2+
#include "syscall.h"
3+
4+
ssize_t read(int fd, void *dst, size_t size) {
5+
return (ssize_t)syscall3((void *)SYS_READ, (void *)fd, dst, (void *)size);
6+
}

userland/cilibc/src/sys/syscall.h

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ extern "C" {
1414

1515
#define SYS_EXIT -1
1616
#define SYS_WRITE 1
17+
#define SYS_READ 2
1718
#define SYS_OPEN 4
1819
#define SYS_CLOSE 8
1920
#define SYS_BRK 128
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.section .text
2+
3+
.global _start
4+
_start:
5+
movq $0, %rbp
6+
pushq %rbp
7+
pushq %rbp
8+
movq %rsp, %rbp
9+
10+
pushq %rsi
11+
pushq %rdi
12+
13+
call _init
14+
15+
call init_stdlib
16+
17+
popq %rdi
18+
popq %rsi
19+
20+
mov (%rsp), %edi
21+
lea 8(%rsp), %rsi
22+
lea 16(%rsp, %rdi, 8), %rdx
23+
xor %eax, %eax
24+
25+
call main
26+
27+
movl %eax, %edi
28+
call exit
29+
30+
.size _start, . - _start

userland/ciutils/hw/hw.main.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ int main(int argc, char* argv[]) {
66
puts("Hello World!\nFrom the libc!\n");
77

88
printf("Hello %s\n", "printf");
9-
printf("Numbers! %i\n", 1234);
9+
printf("Numbers! %i\n", 12345);
1010

1111
return 0;
1212
}

userland/tests/comp.c

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#include <fcntl.h>
2+
#include <stdio.h>
3+
#include <unistd.h>
4+
5+
#define WIDTH 1024
6+
#define HEIGHT 768
7+
#define BPP 4
8+
9+
void fill_rect(unsigned char *fb, int x, int y, int width, int height,
10+
unsigned int color) {
11+
for (int py = y; py < y + height; py++) {
12+
for (int px = x; px < x + width; px++) {
13+
int offset = (py * WIDTH + px) * BPP;
14+
15+
fb[offset] = (color >> 24) & 0xFF;
16+
fb[offset + 1] = (color >> 16) & 0xFF;
17+
fb[offset + 2] = (color >> 8) & 0xFF;
18+
fb[offset + 3] = color & 0xFF;
19+
}
20+
}
21+
}
22+
23+
struct fbinfo {
24+
long int width;
25+
long int height;
26+
};
27+
28+
int main(int argc, char *argv[]) {
29+
puts("Hello World!\nFrom the libc!\n");
30+
31+
printf("Hello %s\n", "printf");
32+
printf("Numbers! %i\n", 1234);
33+
34+
printf("\n");
35+
36+
printf("argc=%d\n", argc);
37+
printf("argv[0]=%s\n", argv[0]);
38+
39+
printf("argv[0][0]=%c\n", argv[0][0]);
40+
41+
FILE *f = fopen("/ext2/test.txt", "r");
42+
43+
fclose(f);
44+
45+
int fb = open("/Devices/Framebuffer", O_RDWR);
46+
47+
struct fbinfo fbinfo;
48+
read(fb, &fbinfo, sizeof(struct fbinfo));
49+
50+
unsigned char buf[WIDTH * HEIGHT * BPP];
51+
52+
printf("width = %d, height = %d\n", fbinfo.width, fbinfo.height);
53+
54+
while (1) {
55+
fill_rect(buf, 100, 100, 200, 150, 0xFF0000FF);
56+
57+
write(fb, buf, WIDTH * HEIGHT * BPP);
58+
}
59+
60+
close(fb);
61+
62+
return 0;
63+
}

0 commit comments

Comments
 (0)