ref: 4ac578a191635f9728dc66dd99b22cc54692b4ef
parent: 3d71446ecb6569add0d9eaf2e7e4a05d99bd2625
author: Ori Bernstein <[email protected]>
date: Sun Apr 22 15:29:39 EDT 2018
Add a special exit stack to our thread library. Because OpenBSD wants a valid stack pointer in any code that enters the kernel, unmapping our stack as part of exiting the process is rather unreliable. This change allocates a stack page that we can switch to when a thread is deallocating itself, which keeps the kernel happy.
--- a/6/insns.def
+++ b/6/insns.def
@@ -287,7 +287,7 @@
/* branch instructions */
Insn(Icall,
- "\tcall %v\n",
+ "\tcall %v@PLT\n",
"\tCALL %V\n",
Use(.l={1}, .r={
Rrdi, Rrsi, Rrdx, Rrcx, Rr8, Rr9,
--- a/lib/sys/sys+openbsd:6.1-x64.myr
+++ b/lib/sys/sys+openbsd:6.1-x64.myr
@@ -272,6 +272,7 @@
const Mfixed : mopt = 0x10
const Mfile : mopt = 0x0
const Manon : mopt = 0x1000
+ const Mstack : mopt = 0x4000
const Mnoreplace : mopt = 0x0800
/* file types */
--- a/lib/thread/exit+openbsd-x64.s
+++ b/lib/thread/exit+openbsd-x64.s
@@ -10,6 +10,13 @@
andq $~0xfff,%rdi /* align it */
addq $0x1000,%rdi
+ /*
+ Because OpenBSD wants a valid stack whenever
+ we enter the kernel, we need to toss a preallocated
+ stack pointer into %rsp.
+ */
+ movq thread$exitstk,%rsp
+
/* munmap(base, size) */
movq $73,%rax /* munmap */
movq -8(%rdi),%rsi /* size */
--- a/lib/thread/spawn+openbsd.myr
+++ b/lib/thread/spawn+openbsd.myr
@@ -5,10 +5,16 @@
type tid = uint64
const spawn : (fn : (-> void) -> std.result(tid, byte[:]))
+ pkglocal var exitstk : byte#
;;
const Stacksz = 8*std.MiB
extern const exit : (-> void)
+
+var exitstk
+const __init__ = {
+ exitstk = getstk(16)
+}
const spawn = {fn;
-> spawnstk(fn, Stacksz)
--- a/lib/thread/test/spawn.myr
+++ b/lib/thread/test/spawn.myr
@@ -21,5 +21,6 @@
;;
std.assert(capture == 333, "capture wasn't written to correctly\n")
+ std.usleep(100_000)
}