Find the answer to your Linux question:
Results 1 to 6 of 6
Hi. I am trying to make a threads using sys_clone. Unfortunately there is no information about some details: 1. How I have to allocate memory for the thread stack? It ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Oct 2012
    Location
    Bulgaria
    Posts
    4

    Make threads with sys_clone


    Hi.
    I am trying to make a threads using sys_clone.
    Unfortunately there is no information about some details:
    1. How I have to allocate memory for the thread stack? It is clear that sys_mmap have to be used, but what flags have to be set in order to provide auto growing of the memory block?

    2. How the allocated memory have to be free and when - in both threads - the thread that creates new thread and the new thread itself.

    3. What flags have to be set in sys_clone call in order to make "real thread", i.e. process that exists in the same space with the main program and have access to all resources of the parent process.

    I tried with following settings, but it seems I have some mistakes (maybe not in the bellow settings) because the so created threads can't work stable with XLib probably because of synchronization issues.

    sys_mmap:
    MAP_ANONYMOUS + MAP_PRIVATE + MAP_GROWSDOWN + MAP_STACK
    PROT_READ + PROT_WRITE + PROT_EXEC

    sys_clone:
    CLONE_SIGHAND + CLONE_THREAD + CLONE_FILES + CLONE_FS + CLONE_VM + CLONE_PTRACE + CLONE_PARENT + CLONE_IO

  2. #2
    Just Joined!
    Join Date
    Oct 2012
    Location
    Bulgaria
    Posts
    4
    No one knows? Or no one cares?
    Come on people, share your secrets!
    Here is the code I use now. It is buggy, but partially works:
    Code:
    proc ThreadCreate, .ptr_to_function, .ptr_to_args
    begin
            push    ebx ecx edx esi edi
    
            push    ebp
            mov     eax, sys_mmap_pgoff
            xor     ebx, ebx
            mov     ecx, __ThreadStackSize
            mov     edx, PROT_READ or PROT_WRITE or PROT_EXEC
            mov     esi, MAP_ANONYMOUS or MAP_PRIVATE or MAP_GROWSDOWN or MAP_STACK
            mov     edi, -1
            xor     ebp, ebp
            int $80
            pop     ebp
    
            cmp     eax, $ffffff00
            jae     .error
    
            lea     ecx, [eax+__ThreadStackSize-8]
    
    ; transfer arguments in the new stack.
            mov     eax, [.ptr_to_function]
            mov     [ecx], eax
            mov     eax, [.ptr_to_args]
            mov     [ecx+4], eax
    
            mov     eax, sys_clone
            mov     ebx, CLONE_SIGHAND or CLONE_THREAD or CLONE_FILES or        \
                         CLONE_FS or CLONE_VM or CLONE_PTRACE or CLONE_PARENT or CLONE_IO
    
            int 0x80
    
            test    eax, eax
            js      .error
            jz      .is_clone
    
            clc
            pop     edi esi edx ecx ebx
            return
    
    ; The stack is not allocated or sys_clone exits with error.
    .error:
            stc
            pop     edi esi edx ecx ebx
            return
    
    ; this is the clone process - call the thread.
    .is_clone:
            pop     eax
            call    eax       ; the argument is already in the stack.
            mov     ebx, eax  ; the thread procedure can return exit code. Save it in ebx.
    
    ; and exit the process  - the question is - how to free the stack allocated...
            mov     eax, sys_exit
            int     0x80
    endp
    Any help or links for more information will be highly appreciated.

    Regards

  3. #3
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by johnfound View Post
    No one knows? Or no one cares?
    Come on people, share your secrets!
    Honestly, that's pretty close to the truth for most people. Pthreads is much easier to use, and more portable (sys_clone is Linux-specific). There is no reason for an application programmer to use it. I assume pthreads uses it - you might want to get the source and see, that might give you some pointers.

    Also, I'm assuming you want to use sys_clone as some sort of intellectual/classroom exercise - otherwise, I highly suggest you use pthreads
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

  4. #4
    Just Joined!
    Join Date
    Oct 2012
    Location
    Bulgaria
    Posts
    4
    Quote Originally Posted by JohnGraham View Post
    I assume pthreads uses it - you might want to get the source and see, that might give you some pointers.
    It was my idea as well, but at first, I don't know C/C++ enough to dig in such a sources and second, I tried, but simply didn't found anything useful. There are thousands of pthread related files in Internet with so many abstraction levels, that I never was able to reach the lines that uses sys_clone (but it must be called somewhere!)

    Also, I'm assuming you want to use sys_clone as some sort of intellectual/classroom exercise - otherwise, I highly suggest you use pthreads
    No, it is not exercise. It is long talk, but I will try to explain.
    I am writing portable library for assembly programming (search FreshLib in google). Now, for Linux, I am using pthreads and libc and it works very good. But besides of this variant, I want to have an option to not depend on external libraries. Sometimes it is very useful.

    For example, 64bit Linux can run 32bit applications if they does not depend on C libraries. Otherwise 32bit libraries must be installed. But it is not always possible. (for example 32bit CGI script on 64bit Linux hosting probably will not run, if uses libc or pthreads but will run properly if uses only system calls and I can't force the hosting provider to install compatibility packages).

  5. #5
    Linux Newbie
    Join Date
    Mar 2010
    Posts
    152
    Quote Originally Posted by johnfound View Post
    I am writing portable library for assembly programming (search FreshLib in google). Now, for Linux, I am using pthreads and libc and it works very good. But besides of this variant, I want to have an option to not depend on external libraries. Sometimes it is very useful.

    For example, 64bit Linux can run 32bit applications if they does not depend on C libraries. Otherwise 32bit libraries must be installed. But it is not always possible. (for example 32bit CGI script on 64bit Linux hosting probably will not run, if uses libc or pthreads but will run properly if uses only system calls and I can't force the hosting provider to install compatibility packages).
    In this case, I'd seriously consider using pthreads and providing an option to disable thread support entirely. It will make your code easier to maintain, both for yourself and others. And a Linux system (which you must be building for if you're going to use sys_clone()) without pthreads would be very unusual.

    Otherwise, have you considered using strace to see what pthreads does? If it helps, a quick run of a simple program that starts a thread with default attributes informs me the clone() call performed is as follows:

    Code:
    clone(child_stack=0xb7522464, flags=CLONE_VM|CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_THREAD|CLONE_SYSVSEM|CLONE_SETTLS|CLONE_PARENT_SETTID|CLONE_CHILD_CLEARTID, parent_tidptr=0xb7522ba8, {entry_number:6, base_addr:0xb7522b40, limit:1048575, seg_32bit:1, contents:0, read_exec_only:0, limit_in_pages:1, seg_not_present:0, useable:1}, child_tidptr=0xb7522ba8) = 5982
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

  6. #6
    Just Joined!
    Join Date
    Oct 2012
    Location
    Bulgaria
    Posts
    4
    Quote Originally Posted by JohnGraham View Post
    In this case, I'd seriously consider using pthreads and providing an option to disable thread support entirely. It will make your code easier to maintain, both for yourself and others. And a Linux system (which you must be building for if you're going to use sys_clone()) without pthreads would be very unusual.
    Well, it is not so uncommon. 64bit Linux has no 32bit pthreads library by default, but has 32bit sys_clone.
    And you know, pthreads is also library as any other. Created and maintained by humans, not by gods.


    Otherwise, have you considered using strace to see what pthreads does?
    Thanks, this advice is really helpful. As I said I am beginner in Linux programming and some tricks, usual for every Linux user are unknown for me. I will make some tests now and if I find something useful will post it here. (Isn't it strange to reverse open source product as an easier way to find how it works).

    Regards.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •