libk  Check-in [f9bf0d662b]

Overview
Comment:partially unfuck x86-32, correct major error in syscall ABI
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: f9bf0d662bf0f71e953e9e678cdf0118abac12e834788c30f5be5660b50d285a
User & Date: lexi on 2019-08-20 02:03:37
Other Links: manifest | tags
Context
2019-08-20
03:04
address case where syscall numbers in header derive from deeper includes check-in: 09a1767049 user: lexi tags: trunk
02:03
partially unfuck x86-32, correct major error in syscall ABI check-in: f9bf0d662b user: lexi tags: trunk
2019-08-19
23:57
add detection loop for syscall headers check-in: c7732c41c9 user: lexi tags: trunk
Changes

Modified arch/x86.lin.32.s from [a7ffc6f8bf] to [30df0fa7c9].























>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
; register order for syscall convention
%define sys.reg.n 6
%define sys.reg.ret eax
%define sys.reg.0 eax
%define sys.reg.1 ebx
%define sys.reg.2 ecx
%define sys.reg.3 edx
%define sys.reg.4 esi
%define sys.reg.5 edi

%define sys.call int 0x80

Modified arch/x86.syscall.64.s from [b26026b9a4] to [43eb022a86].

25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

; syscall ops
%define sys.call syscall

; register order for syscall convention
%define sys.reg.n 7
%define sys.reg.ret rax
%define sys.reg.err rbx
%define sys.reg.0 rax
%define sys.reg.1 rdi
%define sys.reg.2 rsi
%define sys.reg.3 rdx
%define sys.reg.4 r10
%define sys.reg.5 r8
%define sys.reg.6 r9








<








25
26
27
28
29
30
31

32
33
34
35
36
37
38
39

; syscall ops
%define sys.call syscall

; register order for syscall convention
%define sys.reg.n 7
%define sys.reg.ret rax

%define sys.reg.0 rax
%define sys.reg.1 rdi
%define sys.reg.2 rsi
%define sys.reg.3 rdx
%define sys.reg.4 r10
%define sys.reg.5 r8
%define sys.reg.6 r9

Modified build.sh from [a37c2dbfb1] to [15fc50281c].

66
67
68
69
70
71
72
73

74
75
76
77
78
79
80
...
131
132
133
134
135
136
137
































138

















139

140
141
142
143
144






145
146
147
148
149
150
151
152
153
154
155
156
157
158
...
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
...
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
has gcc && _cc=gcc # prefer gcc
cc=${cc:-$_cc}
has m4 && _m4=m4
m4=${m4:-$_m4}
has nasm && asm=nasm
has yasm && asm=yasm # prefer yasm
export gen=${gen:-gen}
library=${libkind:-static} # {static|shared|both}


doc=${doc:-yes}
export doc_html=${doc_html:-yes}
export doc_pdf=${doc_pdf:-yes}
export doc_man=${doc_man:-yes}

case $library in
................................................................................
macro_compile_env="-Datom_target_arch=$arch -Datom_target_os=$os -Dtarget_posix=$posix -Dtarget_unix=$unix"

if test "$bits" != ""; then
	macro_compile_env="$macro_compile_env -Datom_target_bits=$bits"
fi


































comp_mac() { $m4 $macro_compile_env -I "$gen" $3 "$1" > "$2"; }

















comp_asm() { $asm "-f$bin_fmt" -i "$gen" $1 -o "$2"; }

comp_co()  { comp_c $1 $2 "-c -fPIC"; }
comp_c(){
	src=$1
	output=$2
	flags=$3






	$cc $src $3 -std=c11 -isystem "$out" -isystem "$gen" -isystem "arch/" -nostdlib "-L$out" "-o$output"
}

scan() { find "$1" -name "$2"; }

say "commencing libk build $build at $(timestamp)"
set -x

# get type data
mkdir -p $gen
$cc -D_emit_m4_include arch/typesize.c -o $gen/typesize 
$gen/typesize > gen/typesize.m

# generate syscall tables
................................................................................

if test "$doc" = "yes"; then
	global/build-manpage.sh libk.md
	for mod in ${modules[@]}; do
		for doc in $(scan $mod '*.md'); do
			base="$(basename $doc)"
			stem="${base%%.md}"
			global/build-manpage.sh "$doc"
		done
	done
fi

# third pass: compile sources and save the
# resulting object files to $out, tracking
# which is a runtime or function unit. exe's
................................................................................
done

# fourth pass: link the libraries that are
# configured to be built

if test $build_static_library == yes; then
	for obj in ${fn_objects[@]} ${rt_objects[@]} ${data_objects[@]}; do
		ar rc $out/libk.a $obj
	done
	ranlib $out/libk.a
fi

if test $build_shared_library == yes; then
	ld -r ${rt_objects[@]} -o $out/boot.o
	ld -shared ${fn_objects[@]} -o $out/libk.so
fi

# fifth pass: compile the executable tools
# against the libraries created in pass 5

for mod in ${modules[@]}; do
	for exe in $(scan $mod '*.exe.c'); do







|
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>


|
|
|
>
>
>
>
>
>
|





|







 







|







 







|

|



|
|







66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
...
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
...
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
...
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
has gcc && _cc=gcc # prefer gcc
cc=${cc:-$_cc}
has m4 && _m4=m4
m4=${m4:-$_m4}
has nasm && asm=nasm
has yasm && asm=yasm # prefer yasm
export gen=${gen:-gen}
library=${library:-static} # {static|shared|both}
export verbose=${verbose:-quiet} # {no|quiet|loud}

doc=${doc:-yes}
export doc_html=${doc_html:-yes}
export doc_pdf=${doc_pdf:-yes}
export doc_man=${doc_man:-yes}

case $library in
................................................................................
macro_compile_env="-Datom_target_arch=$arch -Datom_target_os=$os -Dtarget_posix=$posix -Dtarget_unix=$unix"

if test "$bits" != ""; then
	macro_compile_env="$macro_compile_env -Datom_target_bits=$bits"
fi


if test "$COLORTERM" != ""; then
	announce() {
		test "$verbose" = "silent" && return
		color="$1" cmd="$2"; shift 2;
		printf " → [38;5;$color;1m$cmd"
		for a in "$@"; do
			if test ${a:0:1} = "-"; then
				printf " $a";
			else
				printf " $a";
			fi
		done
		echo
	}
else
	announce() { shift; echo " --> " $@; }
fi

# the following function is called to report a command invocation
# the person compiling the library. the first argument should be
# an ansi format string; this is used to color-code the tool being
# launched and thus should be different for each one.
report() { announce $@; shift; $@; }
comp_mac() {
	local src=$1
	local output=$2
	local flags=$3
	if test -e "$output"; then
		if test ! "$output" -ot "$src"; then
			return
		fi
	fi
	         $m4 $macro_compile_env -I "$gen" $flags "$src"  > "$output"
	announce 207 $m4 $macro_compile_env -I "$gen" $flags "$src" \> "$output"
	# yes, this is incredibly stupid. if you know a better way, feel
	# free to submit a fix. the problem is there's no way to pass >
	# to report in such a way that it'll do the right thing, and if
	# you just write > it redirects *report's* output, instead of
	# m4's. piece of shit that it is, m4 doesn't have any way to emit
	# output into a fille - stdout only apparently. tl;dr i hate bash.
}
comp_asm() {
	local src=$1
	local output=$2
	local flags=$3
	if test -e "$output"; then
		if test ! "$output" -ot "$src"; then
			return
		fi
	fi
	report 198 $asm $flags "-f$bin_fmt" -i "$gen" "$src" -o "$output";
}
comp_co()  { comp_c $1 $2 "-c -fPIC"; }
comp_c(){
	local src=$1
	local output=$2
	local flags=$3
	if test -e "$output"; then
		if test ! "$output" -ot "$src"; then
			return
		fi
	fi
	# only rebuild the file if the source file is newer
	report 213 $cc $src $3 -std=c11 -isystem "$out" -isystem "$gen" -isystem "arch/" -nostdlib "-L$out" "-o$output"
}

scan() { find "$1" -name "$2"; }

say "commencing libk build $build at $(timestamp)"
# set -x

# get type data
mkdir -p $gen
$cc -D_emit_m4_include arch/typesize.c -o $gen/typesize 
$gen/typesize > gen/typesize.m

# generate syscall tables
................................................................................

if test "$doc" = "yes"; then
	global/build-manpage.sh libk.md
	for mod in ${modules[@]}; do
		for doc in $(scan $mod '*.md'); do
			base="$(basename $doc)"
			stem="${base%%.md}"
			report 177 global/build-manpage.sh "$doc"
		done
	done
fi

# third pass: compile sources and save the
# resulting object files to $out, tracking
# which is a runtime or function unit. exe's
................................................................................
done

# fourth pass: link the libraries that are
# configured to be built

if test $build_static_library == yes; then
	for obj in ${fn_objects[@]} ${rt_objects[@]} ${data_objects[@]}; do
		report 120 ar rc $out/libk.a $obj
	done
	report 105 ranlib $out/libk.a
fi

if test $build_shared_library == yes; then
	report 216 ld -r ${rt_objects[@]} -o $out/boot.o
	report 216 ld -shared ${fn_objects[@]} -o $out/libk.so
fi

# fifth pass: compile the executable tools
# against the libraries created in pass 5

for mod in ${modules[@]}; do
	for exe in $(scan $mod '*.exe.c'); do

Modified global/build-manpage.sh from [790e90297c] to [5a75b066a8].

51
52
53
54
55
56
57

58
59
60
61
62
63

64
65
66
67
68
69
70
71
72
73
74
75
76

77
78
echo >>"$fmt" ".SH DESCRIPTION"

tail -n+$(expr $descline + $offset) "$file"  | cmark -t man >> "$fmt"

test "$doc_html" = "yes" && {
	mkdir -p "$htmldest"
	groff -Thtml -Kutf8 -m man "$fmt" > "$html"

	say "wrote html page for $stem to $html"
}

test "$doc_pdf" = "yes" && {
	mkdir -p "$pdfdest"
	groff -Tpdf  -Kutf8 -m man "$fmt" > "$pdf"

	say "wrote pdf for $stem to $pdf"
}

test "$doc_man" = "yes" && {
	mkdir -p "$mandest"

	if has gzip; then
		man="$man.gz"
		gzip -c -f "$fmt" > "$man"
	else
		mv "$fmt" "$man"
	fi


	say "wrote manpage for $stem to $man"
}







>






>













>


51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
echo >>"$fmt" ".SH DESCRIPTION"

tail -n+$(expr $descline + $offset) "$file"  | cmark -t man >> "$fmt"

test "$doc_html" = "yes" && {
	mkdir -p "$htmldest"
	groff -Thtml -Kutf8 -m man "$fmt" > "$html"
	test "$verbose" != "loud" ||
	say "wrote html page for $stem to $html"
}

test "$doc_pdf" = "yes" && {
	mkdir -p "$pdfdest"
	groff -Tpdf  -Kutf8 -m man "$fmt" > "$pdf"
	test "$verbose" != "loud" ||
	say "wrote pdf for $stem to $pdf"
}

test "$doc_man" = "yes" && {
	mkdir -p "$mandest"

	if has gzip; then
		man="$man.gz"
		gzip -c -f "$fmt" > "$man"
	else
		mv "$fmt" "$man"
	fi

	test "$verbose" != "loud" ||
	say "wrote manpage for $stem to $man"
}

Modified kcore/syscall.fn.x86.lin.64.s from [f9a360108d] to [27d79c3e63].

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66





67
68
69
70
71








72
73
	%assign i 0
	%rep 6
		handle_arg i
		%assign i i+1
	%endrep

	; valency >= 7. this is not valid, so
	; we set our return value to 0 and the
	; error number to its maximum value in
	; order to indicate that the syscall
	; was invalid
	mov qword [rbx], 0
	mov qword [r12], -1
	ret

	; we have a valency match - perform the
	; requested syscall already store in rax
	.perform_call: sys.call





	
	; move our return values into place and
	; return to the caller (which should
	; always be k_platform_syscall, btw)
	mov [rbx], sys.reg.ret








	mov [r12], sys.reg.err
	ret







|



|






>
>
>
>
>




|
>
>
>
>
>
>
>
>
|
|
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
	%assign i 0
	%rep 6
		handle_arg i
		%assign i i+1
	%endrep

	; valency >= 7. this is not valid, so
	; we set our return value to -1 and the
	; error number to its maximum value in
	; order to indicate that the syscall
	; was invalid
	mov qword [rbx], -1
	mov qword [r12], -1
	ret

	; we have a valency match - perform the
	; requested syscall already store in rax
	.perform_call: sys.call

	; check for an error - on x86, error is
	; returned as a negative value in %rax
	test sys.reg.ret, sys.reg.ret
	js .error ; jump if sign flag set
	
	; move our return values into place and
	; return to the caller (which should
	; always be k_platform_syscall, btw)
	mov       [rbx], sys.reg.ret
	mov qword [r12], 0 ; no error
	ret

	; an error was returned - we need to set
	; the errno to its positive equivalent,
	; and store -1 in the return variable
	.error: neg sys.reg.ret
	        mov qword [rbx], -1
			mov       [r12], sys.reg.ret
			ret

Name change from kcore/exit.fn.x86.lin.32.s to legacy/exit.fn.x86.lin.32.s.

Name change from kcore/exit.fn.x86.lin.64.s to legacy/exit.fn.x86.lin.64.s.

Modified libk.md from [a982938576] to [a1a4077789].

173
174
175
176
177
178
179

180
181
182
183
184
185
186
 * `cc=<autodetect> {executable}` - the compiler to compile C sources with
 * `m4=<autodetect> {executable}` - the m4 binary to compile the macro sources with
 * `asm=<autodetect> {executable}` - the assembler to assemble the assembly listings with. it must take Intel-syntax input and handle nasm-style macros. only `yasm` and `nasm` are likely to be viable.
 * `doc=yes {yes|no}` - whether to typeset the documentation (very slow with all three formats set to "yes")
 * `doc_html=yes {yes|no}` - enable or disable html output of the documentation
 * `doc_pdf=yes {yes|no}` - enable or disable pdf output of the documentation
 * `doc_man=yes {yes|no}` - enable or disable manpage output of the documentation


two other shell scripts complete the build system:
 * `install.sh` - installs compiled libraries, objects, documentation, and headers into the appropriate directories.

# design principles

there are four overriding principles that guide the design of libk.







>







173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
 * `cc=<autodetect> {executable}` - the compiler to compile C sources with
 * `m4=<autodetect> {executable}` - the m4 binary to compile the macro sources with
 * `asm=<autodetect> {executable}` - the assembler to assemble the assembly listings with. it must take Intel-syntax input and handle nasm-style macros. only `yasm` and `nasm` are likely to be viable.
 * `doc=yes {yes|no}` - whether to typeset the documentation (very slow with all three formats set to "yes")
 * `doc_html=yes {yes|no}` - enable or disable html output of the documentation
 * `doc_pdf=yes {yes|no}` - enable or disable pdf output of the documentation
 * `doc_man=yes {yes|no}` - enable or disable manpage output of the documentation
 * `verbose=quiet {silent|quiet|loud}` - control level of verbosity. `silent` silences most output.

two other shell scripts complete the build system:
 * `install.sh` - installs compiled libraries, objects, documentation, and headers into the appropriate directories.

# design principles

there are four overriding principles that guide the design of libk.