0

First of all a small intro: I am developing a TCP server in Assembler x64 with NASM, all good until there, implement the fork system call so that each client that connects is not on the waiting list, in short, that it works and I have it quite clear. Now, I wanted to implement threads, in the same way that it is done in C with pthread but pum in the list of system calls neither pthread_create, nor pthread_join nor anything similar to what is in C exist, but if it exists in C, somehow it is done in Assembler.

Reading a little, apparently a thread would be created by the sys_clone system call, now my problem is that I have no idea what its arguments refer to and I can't find examples anywhere of its implementation, if you know of any website that explains each argument of the system calls in Linux I would appreciate it because it asks me something like this int __user * parent_tidptr and makes him realize that they are speaking to me in Russian.

mov rdi, ???    ; unsigned long clone_flags
mov rsi, ???    ; unsigned long newsp
mov rdx, ???    ; int __user * parent_tidptr
mov r10, ???    ; int __user * child_tidptr
mov r8,  ???    ; int tls_val
mov rax, 56
syscall

The list of system calls with their respective arguments I get them from this site: link

In Linux Programmer's Manual I try to guide myself with this: link, but I am in the same, I do not understand the arguments, the first argument I suppose is like in pthread_create that you pass a function but the truth I do not know.

Without more to say I thank you in advance, and sorry if some things seem obvious, but I have been doing this for at most 2 weeks, which I love but we must admit that finding Assembler information is not as simple as looking for C things, C++, Javascript haha, among others. Bye if you read this I love you!

Peter Cordes
  • 328,167
  • 45
  • 605
  • 847
  • 1
    *I can't find examples anywhere of its implementation* - One obvious user of `clone` is libpthread itself. Use `strace` on a multi-threaded program to see what it does, and/or read the pthread source code. Also the man page (https://man7.org/linux/man-pages/man2/clone.2.html) is a lot to go through, but is clearly aimed at how to use it from user-space, so there's no mention of kernel stuff like `__user` decorations on the by-reference output args or other pointer args. – Peter Cordes Apr 10 '21 at 05:13
  • If you're going to be coding in assembly then you want to be reading the part of the man page about the "raw clone() system call", or maybe better `clone3()`, but not about the "glibc clone() wrapper function". In particular the raw system call does not take a function pointer as an argument. – Nate Eldredge Apr 10 '21 at 05:17
  • @NateEldredge: Yes, good point. Specifically the [Notes section](https://man7.org/linux/man-pages/man2/clone.2.html#NOTES) has the "C library/kernel differences" section. Or use `clone3` which takes a pointer to a struct. IIRC, there have been some SO questions about how a new thread finds the `fn`, e.g. parent stores a pointer on the new thread's stack before invoking `clone`. – Peter Cordes Apr 10 '21 at 05:19
  • I think I found something like what I was looking for on this site: [link](https://github.com/skeeto/pure-linux-threads-demo/blob/master/threads-x86_64.s). I will read it and try to implement it, if I find it, I will leave it documented on this site. – Franco Milich Apr 10 '21 at 06:11
  • this answer, and question, has some good information and links: https://stackoverflow.com/a/66863181/14653862 – sol Jan 05 '23 at 00:50

0 Answers0