summaryrefslogtreecommitdiff
path: root/lib/thread/tls-impl+osx-x64.s
diff options
context:
space:
mode:
authoririri <iri@konnichiwastevenspielbergde.su>2018-10-13 13:58:52 -0700
committerOri Bernstein <ori@eigenstate.org>2018-10-18 23:21:10 -0700
commit2d9fe7100209acb431e392d58e4cf4eccfde00b7 (patch)
tree888cd09a78c8d75a53f474f3c4802fb914a6bff7 /lib/thread/tls-impl+osx-x64.s
parentb17873ed4e6a14786758fd68290ef572b0b88f10 (diff)
downloadmc-2d9fe7100209acb431e392d58e4cf4eccfde00b7.tar.gz
Add thread-local storage for POSIX-y platforms.
This patch implements thread-local storage by using a segment register to store pointers to regions of memory unique to each thread. In spawned threads, this region starts above the top of the stack, while in the main thread, this region is initially statically allocated so it can be used in `__init__` functions and dynamically reallocated if it needs to grow beyond 8 slots. The api consists of 3 functions: `tlsalloc()`, which must be called from the main thread, allocates a slot and returns a key to be used with `settls(k, v)` and `gettls(k)`. Each thread inherits its tls slots from the thread that spawned it and any slot added after a given thread is spawned is not available in that thread. Adding tls regions gives threads an easy way to get their own tids, allowing us to add some basic correctness checks to the mutex code. A pointer to the base of the stack and the size of the mapping are also stored in the tls region, making it easy to support user-specified stack sizes in the future. Changes from previous version: - Fixed size of main tls static allocation. - More comments, less magic. - Fixed typo in start+osx-x64.s exit+{freebsd,linux,openbsd}-x64.s - `gettlskey` renamed to `tlsalloc`. - `key` is now `tlskey(@a#)`, improving safety. - Test is slightly less bad.
Diffstat (limited to 'lib/thread/tls-impl+osx-x64.s')
-rw-r--r--lib/thread/tls-impl+osx-x64.s64
1 files changed, 64 insertions, 0 deletions
diff --git a/lib/thread/tls-impl+osx-x64.s b/lib/thread/tls-impl+osx-x64.s
new file mode 100644
index 0000000..bbe7dcd
--- /dev/null
+++ b/lib/thread/tls-impl+osx-x64.s
@@ -0,0 +1,64 @@
+.set tid, 0x00
+.set len, 0x08
+.set self, 0x20
+.set slots, 0x28
+
+/* const tid : (-> tid) */
+.globl thread$tid
+.globl _thread$tid
+thread$tid:
+_thread$tid:
+ movq %gs:tid, %rax
+ ret
+
+/* const _tlsset : (k : key, v : void# -> void) */
+.globl thread$_tlsset
+.globl _thread$_tlsset
+thread$_tlsset:
+_thread$_tlsset:
+ cmpq %gs:len, %rdi
+ jnb oob
+
+ movq $slots, %r10
+ movq %rsi, %gs:(%r10, %rdi, 0x8)
+ ret
+
+/* const _tlsget : (k : key -> void#) */
+.globl thread$_tlsget
+.globl _thread$_tlsget
+thread$_tlsget:
+_thread$_tlsget:
+ cmpq %gs:len, %rdi
+ jnb oob
+
+ movq $slots, %r10
+ movq %gs:(%r10, %rdi, 0x8), %rax
+ ret
+
+oob:
+ call _thread$tlsoob
+
+/* const tlslen : (-> key) */
+.globl thread$tlslen
+.globl _thread$tlslen
+thread$tlslen:
+_thread$tlslen:
+ movq %gs:len, %rax
+ ret
+
+/* const _setgsbase : (h : tlshdr# -> int64) */
+.globl thread$_setgsbase
+.globl _thread$_setgsbase
+thread$_setgsbase:
+_thread$_setgsbase:
+ movq $0x3000003, %rax /* undocumented syscall; sets %gs to %rdi */
+ syscall
+ ret
+
+/* const getgsbase : (-> tlshdr#) */
+.globl thread$getgsbase
+.globl _thread$getgsbase
+thread$getgsbase:
+_thread$getgsbase:
+ movq %gs:self, %rax
+ ret