1. 10 1月, 2018 2 次提交
    • J
      consistently use the LOCK an UNLOCK macros · c4bc0b1a
      Jens Gustedt 提交于
      In some places there has been a direct usage of the functions. Use the
      macros consistently everywhere, such that it might be easier later on to
      capture the fast path directly inside the macro and only have the call
      overhead on the slow path.
      c4bc0b1a
    • J
      new lock algorithm with state and congestion count in one atomic int · 47d0bcd4
      Jens Gustedt 提交于
      A variant of this new lock algorithm has been presented at SAC'16, see
      https://hal.inria.fr/hal-01304108. A full version of that paper is
      available at https://hal.inria.fr/hal-01236734.
      
      The main motivation of this is to improve on the safety of the basic lock
      implementation in musl. This is achieved by squeezing a lock flag and a
      congestion count (= threads inside the critical section) into a single
      int. Thereby an unlock operation does exactly one memory
      transfer (a_fetch_add) and never touches the value again, but still
      detects if a waiter has to be woken up.
      
      This is a fix of a use-after-free bug in pthread_detach that had
      temporarily been patched. Therefore this patch also reverts
      
               c1e27367
      
      This is also the only place where internal knowledge of the lock
      algorithm is used.
      
      The main price for the improved safety is a little bit larger code.
      
      Under high congestion, the scheduling behavior will be different
      compared to the previous algorithm. In that case, a successful
      put-to-sleep may appear out of order compared to the arrival in the
      critical section.
      47d0bcd4
  2. 14 10月, 2017 1 次提交
    • R
      fix read-after-free type error in pthread_detach · c1e27367
      Rich Felker 提交于
      calling __unlock on t->exitlock is not valid because __unlock reads
      the waiters count after making the atomic store that could allow
      pthread_exit to continue and unmap the thread's stack and the object t
      points to. for now, inline the __unlock logic with an unconditional
      futex wake operation so that the waiters count is not needed.
      
      once __lock/__unlock have been made safe for self-synchronized
      destruction, we could switch back to using them.
      c1e27367
  3. 07 9月, 2017 1 次提交
    • R
      fix signal masking race in pthread_create with priority attributes · 9e01be6e
      Rich Felker 提交于
      if the parent thread was able to set the new thread's priority before
      it reached the check for 'startlock', the new thread failed to restore
      its signal mask and thus ran with all signals blocked.
      
      concept for patch by Sergei, who reported the issue; unnecessary
      changes were removed and comments added since the whole 'startlock'
      thing is non-idiomatic and confusing. eventually it should be replaced
      with use of idiomatic synchronization primitives.
      9e01be6e
  4. 12 8月, 2017 1 次提交
    • R
      trap UB from attempts to join a detached thread · 80bf5952
      Rich Felker 提交于
      passing to pthread_join the id of a thread which is not joinable
      results in undefined behavior.
      
      in principle the check to trap does not necessarily work if
      pthread_detach was called after thread creation, since no effort is
      made here to synchronize access to t->detached, but the check is
      well-defined and harmless for callers which did not invoke UB, and
      likely to help catch erroneous code that would otherwise mysteriously
      hang.
      
      patch by William Pitcock.
      80bf5952
  5. 05 7月, 2017 1 次提交
    • J
      unify the use of FUTEX_PRIVATE · d906fa31
      Jens Gustedt 提交于
      The flag 1<<7 is used in several places for different purposes that are
      not always easy to distinguish. Mark those usages that correspond to the
      flag that is used by the kernel for futexes.
      d906fa31
  6. 09 6月, 2017 1 次提交
    • R
      use hard-coded sh4a atomic opcodes to avoid linker errors on sh · bc313e88
      Rich Felker 提交于
      when using the sh4a opcodes, the assembler tags the resulting object
      file as requiring sh4a. the linker then refuses to (static) link it
      with object files marked as requiring j2, since there is no isa level
      that includes both sh4a and j2 instructions.
      bc313e88
  7. 16 2月, 2017 1 次提交
    • R
      fix build regression in arm atomics asm with new binutils · b261a242
      Rich Felker 提交于
      binutils commit bada43421274615d0d5f629a61a60b7daa71bc15 tightened
      immediate fixup handling in gas in such a way that the final .arch of
      an object file must be compatible with the fixups used when the
      instruction was assembled; this in turn broke assembling of atomics.s,
      at least in thumb mode.
      
      it's not clear whether this should be considered a bug in gas, but
      .object_arch is preferable anyway for our purpose here of controlling
      the ISA level tag on the object file being produced, and it's the
      intended directive for use in object files with runtime code
      selection. research by Szabolcs Nagy confirmed that .object_arch is
      supported in all relevant versions of binutils and clang's integrated
      assembler.
      
      patch by Reiner Herrmann.
      b261a242
  8. 20 1月, 2017 1 次提交
    • R
      fix spurious EINTR errors from multithreaded set*id, etc. · 6894f847
      Rich Felker 提交于
      commit 78a8ef47 inadvertently removed
      the SA_RESTART flag from the sigaction for the internal signal handler
      used by __synccall for broadcasting. as a result, programs which did
      not use interrupting signals but which used set*id() in a
      multithreaded context could wrongly observe EINTR errors they're not
      prepared to handle.
      6894f847
  9. 13 1月, 2017 1 次提交
    • R
      fix crashes in x32 __tls_get_addr · 1f53e7d0
      rofl0r 提交于
      x32 has another gratuitous difference to all other archs:
      it passes an array of 64bit values to __tls_get_addr().
      usually it is an array of size_t.
      1f53e7d0
  10. 20 12月, 2016 1 次提交
    • R
      rework arm atomic/tp backends to be thumb-compatible and fdpic-ready · 29237f7f
      Rich Felker 提交于
      three problems are addressed:
      
      - use of pc arithmetic, which was difficult if not impossible to make
        correct in thumb mode on all models, so that relative rather than
        absolute pointers to the backends could be used. this was designed
        back when there was no coherent model for the early stages of the
        dynamic linker before relocations, and is no longer necessary.
      
      - assumption that data (the relative pointers to the backends) can be
        accessed at a constant displacement from the code. this will not be
        possible on future fdpic subarchs (for cortex-m), so move
        responsibility for loading the backend code address to the caller.
      
      - hard-coded arm opcodes using the .word directive. instead, use the
        .arch directive to work around the assembler's refusal to assemble
        instructions not available (or in some cases, available but just
        considered deprecated) in the target isa level. the obscure v6t2
        arch is used for v6 code so as to (1) allow generation of thumb2
        output if -mthumb is active, and (2) avoid warnings/errors for mcr
        barriers that clang would produce if we just set arch to v7-a.
      
      in addition, the __aeabi_read_tp function is moved out of the inner
      workings and implemented as an asm wrapper around a C function, so
      that asm code does not need to read global data. the asm wrapper
      serves to satisfy the ABI calling convention requirements for this
      function.
      29237f7f
  11. 12 11月, 2016 1 次提交
  12. 09 11月, 2016 2 次提交
  13. 08 11月, 2016 1 次提交
    • R
      simplify pthread_attr_t stack/guard size representation · 33ce9208
      Rich Felker 提交于
      previously, the pthread_attr_t object was always initialized all-zero,
      and stack/guard size were represented as differences versus their
      defaults. this required lots of confusing offset arithmetic everywhere
      they were used. instead, have pthread_attr_init fill in the default
      values, and work with absolute sizes everywhere.
      33ce9208
  14. 20 10月, 2016 1 次提交
  15. 01 7月, 2016 1 次提交
  16. 28 6月, 2016 1 次提交
    • R
      fix failure to obtain EOWNERDEAD status for process-shared robust mutexes · 384d103d
      Rich Felker 提交于
      Linux's documentation (robust-futex-ABI.txt) claims that, when a
      process dies with a futex on the robust list, bit 30 (0x40000000) is
      set to indicate the status. however, what actually happens is that
      bits 0-30 are replaced with the value 0x40000000, i.e. bits 0-29
      (containing the old owner tid) are cleared at the same time bit 30 is
      set.
      
      our userspace-side code for robust mutexes was written based on that
      documentation, assuming that kernel would never produce a futex value
      of 0x40000000, since the low (owner) bits would always be non-zero.
      commit d338b506 introduced this
      assumption explicitly while fixing another bug in how non-recoverable
      status for robust mutexes was tracked. presumably the tests conducted
      at that time only checked non-process-shared robust mutexes, which are
      handled in pthread_exit (which implemented the documented kernel
      protocol, not the actual one) rather than by the kernel.
      
      change pthread_exit robust list processing to match the kernel
      behavior, clearing bits 0-29 while setting bit 30, and use the value
      0x7fffffff instead of 0x40000000 to encode non-recoverable status. the
      choice of value here is arbitrary; any value with at least one of bits
      0-29 set should work just as well,
      384d103d
  17. 09 5月, 2016 1 次提交
  18. 26 4月, 2016 1 次提交
    • R
      fix thread structure/dtv-pointer corruption on powerpc · be999f7a
      Rich Felker 提交于
      per the powerpc psabi, offset 4 of the stack at call time belongs to
      the callee and is used for spilling lr (return address). in addition,
      offset 0 on the stack must contain a pointer to the previous stack
      frame, or a null pointer for the initial stack frame of a thread.
      __clone failed to setup any stack frame on the new thread's stack,
      thereby allowing the start function it called to clobber offset 4 of
      the new thread's struct __pthread, which contains the dtv pointer.
      
      add code to setup a proper stack frame and align the stack pointer to
      a multiple of 16 (also an abi requirement) if it was not already
      aligned.
      be999f7a
  19. 18 4月, 2016 1 次提交
  20. 07 3月, 2016 1 次提交
    • R
      add mips64 port · 83933573
      Rich Felker 提交于
      patch by Mahesh Bodapati and Jaydeep Patil of Imagination
      Technologies.
      83933573
  21. 09 2月, 2016 1 次提交
    • R
      remove workaround for broken mips assemblers · 869a9df5
      Rich Felker 提交于
      the workaround was for a bug that botched .gpword references to local
      labels, applying a nonsensical random offset of -0x4000 to them.
      
      this reverses commit 5e396fb9 and a
      removes a similar hack that was added to syscall_cp.s in the later
      commit 756c8af8. it turns out one
      additional instance of the same idiom, the GETFUNCSYM macro in
      arch/mips/reloc.h, was still affected by the assembler bug and does
      not admit an easy workaround without making assumptions about how the
      macro is used. the previous workarounds made static linking work but
      left the early-stage dynamic linker broken and thus had limited
      usefulness.
      
      instead, affected users (using binutils versions older than 2.20) will
      need to fix the bug on the binutils side; the trivial patch is commit
      453f5985b13e35161984bf1bf657bbab11515aa4 in the binutils-gdb
      repository.
      869a9df5
  22. 05 2月, 2016 2 次提交
    • R
      in mips cancellable syscall asm, don't assume gp register is valid · 756c8af8
      Rich Felker 提交于
      the old __cp_cancel code path loaded the address of __cancel from the
      GOT using the $gp register, which happened to be set to point to the
      correct GOT by the calling C function, but there is no ABI requirement
      that this happen. instead, go the roundabout way and compute the
      address of __cancel via pc-relative and gp-relative addressing
      starting with a fake return address generated by a bal instruction,
      which is the same trick crt1 uses to bootstrap.
      756c8af8
    • R
      avoid using signals when a thread attempts to cancel itself · aecda353
      Rich Felker 提交于
      not only is pthread_kill expensive in this case; it also breaks
      testing under qemu app-level emulation.
      aecda353
  23. 31 1月, 2016 1 次提交
    • R
      fix misaligned pointer-like objects in arm atomics asm source file · 9ee57db8
      Rich Felker 提交于
      this file's .data section was not aligned, and just happened to get
      the correct alignment with past builds. it's likely that the move of
      atomic.s from arch/arm/src to src/thread/arm caused the change in
      alignment, which broke the atomic and thread-pointer access fragments
      on actual armv5 hardware.
      9ee57db8
  24. 22 1月, 2016 4 次提交
    • R
      230bfe1a
    • R
      move x32 sysinfo impl and syscall fixup code out of arch/x32/src · 66215afc
      Rich Felker 提交于
      all such arch-specific translation units are being moved to
      appropriate arch dirs under the main src tree.
      66215afc
    • R
      move arm-specific translation units out of arch/arm/src, to src/*/arm · e617b9ee
      Rich Felker 提交于
      this is possible with the new build system that allows src/*/$(ARCH)/*
      files which do not shadow a file in the parent directory, and yields a
      more logical organization. eventually it will be possible to remove
      arch/*/src from the build system.
      e617b9ee
    • R
      overhaul sh atomics for new atomics framework, add j-core cas.l backend · 61b1e75f
      Rich Felker 提交于
      sh needs runtime-selected atomic backends since there are a number of
      supported models that use non-forwards-compatible (non-smp-compatible)
      atomic mechanisms. previously, the code paths for this were highly
      inefficient since they involved C function calls with multiple
      branches in the callee and heavy spills in the caller. the new code
      performs calls the runtime-selected asm fragment from inline asm with
      extremely minimal clobbers, rather than using a function call.
      
      for the sh4a case where the atomic mechanism is known and there is no
      forward-compatibility issue, the movli.l and movco.l instructions are
      provided as a_ll and a_sc, allowing the new shared atomic.h to
      generate efficient inline versions of all the basic atomic operations
      without needing a cas loop.
      61b1e75f
  25. 12 11月, 2015 2 次提交
    • R
      eliminate use of SHARED macro in __tls_get_addr · 4aaf879e
      Rich Felker 提交于
      this was only a tiny optimization, and static-linked binaries should
      not be calling __tls_get_addr anyway since the linker is supposed to
      perform relaxation, resulting in use of the local-exec TLS model.
      4aaf879e
    • R
      eliminate use of SHARED macro to suppress visibility attributes · 8a8fdf63
      Rich Felker 提交于
      this is the first and simplest stage of removal of the SHARED macro,
      which will eventually allow libc.a and libc.so to be produced from the
      same object files.
      
      the original motivation for these #ifdefs which are now being removed
      was to allow building a static-only libc using a compiler that does
      not support visibility. however, SHARED was the wrong condition to
      test for this anyway; various assembly-language sources refer to
      hidden symbols and declare them with the .hidden directive, making it
      wrong to define the referenced symbols as non-hidden. if there is a
      need in the future to build libc using compilers that lack visibility,
      support could be moved to the build system or perhaps the __PIC__
      macro could be checked instead of SHARED.
      8a8fdf63
  26. 10 11月, 2015 2 次提交
    • R
      explicitly assemble all arm asm sources as UAL · 4e73d121
      Rich Felker 提交于
      these files are all accepted as legacy arm syntax when producing arm
      code, but legacy syntax cannot be used for producing thumb2 with
      access to the full ISA. even after switching to UAL, some asm source
      files contain instructions which are not valid in thumb mode, so these
      will need to be addressed separately.
      4e73d121
    • R
      remove non-working pre-armv4t support from arm asm · 9f290a49
      Rich Felker 提交于
      the idea of the three-instruction sequence being removed was to be
      able to return to thumb code when used on armv4t+ from a thumb caller,
      but also to be able to run on armv4 without the bx instruction
      available (in which case the low bit of lr would always be 0).
      however, without compiler support for generating such a sequence from
      C code, which does not exist and which there is unlikely to be
      interest in implementing, there is little point in having it in the
      asm, and it would likely be easier to add pre-armv4t support via
      enhanced linker handling of R_ARM_V4BX than at the compiler level.
      
      removing this code simplifies adding support for building libc in
      thumb2-only form (for cortex-m).
      9f290a49
  27. 03 11月, 2015 2 次提交
    • R
      use explicit __cp_cancel label in cancellable syscall asm for all archs · 36e8b6a2
      Rich Felker 提交于
      previously, only archs that needed to do stack cleanup defined a
      __cp_cancel label for acting on cancellation in their syscall asm, and
      a default definition was provided by a weak alias to __cancel, the C
      function. this resulted in wrong codegen for arm on gcc versions
      affected by pr 68178 and possibly similar issues (like pr 66609) on
      other archs, and also created an inconsistency where the __cp_begin
      and __cp_end labels were treated as const data but __cp_cancel was
      treated as a function. this in turn caused incorrect code generation
      on archs where function pointers point to function descriptors rather
      than code (for now, only sh/fdpic).
      36e8b6a2
    • R
      properly access mcontext_t program counter in cancellation handler · cb1bf2f3
      Rich Felker 提交于
      using the actual mcontext_t definition rather than an overlaid pointer
      array both improves correctness/readability and eliminates some ugly
      hacks for archs with 64-bit registers bit 32-bit program counter.
      
      also fix UB due to comparison of pointers not in a common array
      object.
      cb1bf2f3
  28. 16 10月, 2015 1 次提交
    • B
      add missing memory barrier to pthread_join · 53cd8c5a
      Bobby Bingham 提交于
      POSIX requires pthread_join to synchronize memory on success.  The
      futex wait inside __timedwait_cp cannot handle this because it's not
      called in all cases.  Also, in the case of a spurious wake, tid can
      become zero between the wake and when the joining thread checks it.
      53cd8c5a
  29. 12 9月, 2015 1 次提交
    • R
      make sh clone asm fdpic-compatible · 234c5846
      Rich Felker 提交于
      clone calls back to a function pointer provided by the caller, which
      will actually be a pointer to a function descriptor on fdpic. the
      obvious solution is to have a separate version of clone for fdpic, but
      I have taken a simpler approach to go around the problem. instead of
      calling the pointed-to function from asm, a direct call is made to an
      internal C function which then calls the pointed-to function. this
      lets the C compiler generate the appropriate calling convention for an
      indirect call with no need for ABI-specific assembly.
      234c5846
  30. 26 6月, 2015 1 次提交
    • R
      fix local-dynamic model TLS on mips and powerpc · 6ba5517a
      Rich Felker 提交于
      the TLS ABI spec for mips, powerpc, and some other (presently
      unsupported) RISC archs has the return value of __tls_get_addr offset
      by +0x8000 and the result of DTPOFF relocations offset by -0x8000. I
      had previously assumed this part of the ABI was actually just an
      implementation detail, since the adjustments cancel out. however, when
      the local dynamic model is used for accessing TLS that's known to be
      in the same DSO, either of the following may happen:
      
      1. the -0x8000 offset may already be applied to the argument structure
      passed to __tls_get_addr at ld time, without any opportunity for
      runtime relocations.
      
      2. __tls_get_addr may be used with a zero offset argument to obtain a
      base address for the module's TLS, to which the caller then applies
      immediate offsets for individual objects accessed using the local
      dynamic model. since the immediate offsets have the -0x8000 adjustment
      applied to them, the base address they use needs to include the
      +0x8000 offset.
      
      it would be possible, but more complex, to store the pointers in the
      dtv[] array with the +0x8000 offset pre-applied, to avoid the runtime
      cost of adding 0x8000 on each call to __tls_get_addr. this change
      could be made later if measurements show that it would help.
      6ba5517a
  31. 20 6月, 2015 1 次提交
    • R
      work around mips detached thread exit breakage due to kernel regression · ce3688ec
      Rich Felker 提交于
      linux kernel commit 46e12c07b3b9603c60fc1d421ff18618241cb081 caused
      the mips syscall mechanism to fail with EFAULT when the userspace
      stack pointer is invalid, breaking __unmapself used for detached
      thread exit. the workaround is to set $sp to a known-valid, readable
      address, and the simplest one to obtain is the address of the current
      function, which is available (per o32 calling convention) in $25.
      ce3688ec