Commit Graph

778 Commits

Author SHA1 Message Date
Brandon Bergren
bdccf0bbac Implement _umtx_op_err.S on powerpc / powerpc64.
As per r177853, we need to avoid using errno inside user mutex code, since
 signal handlers can interfere with it and mess up libthr internal state.

So, implement _umtx_op_err() instead, which makes a raw syscall and
returns the error value directly instead of using errno.

Approved by: jhibbits (mentor)
Differential Revision: https://reviews.freebsd.org/D20946
2019-08-09 20:16:38 +00:00
Konstantin Belousov
3638455c92 Avoid conflicts with libc symbols in libthr jump table.
In some corner cases of static linking and unexpected libraries order
on the linker command line, libc symbol might preempt the same libthr
symbol, in which case libthr jump table points back to libc causing
either infinite recursion or loop.  Handle all of such symbols by
using private libthr names for them, ensuring that the right pointers
are installed into the table.

In collaboration with: arichardson
PR:	239475
Tested by:	pho
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D21088
2019-07-31 20:04:39 +00:00
Konstantin Belousov
0ab1bfc7b2 Avoid conflicts with libc symbols in libthr jump table.
In some corner cases of static linking and unexpected libraries order
on the linker command line, libc symbol might preempt the same libthr
symbol, in which case libthr jump table points back to libc causing
either infinite recursion or loop.  Handle all of such symbols by
using private libthr names for them, ensuring that the right pointers
are installed into the table.

In collaboration with: arichardson
PR:	239475
Tested by:	pho
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D21088
2019-07-31 19:27:20 +00:00
Konstantin Belousov
3212ada519 Restore ability to pass NULL name argument to pthread_set_name_np(3)
to clear the thread name.

PR:	239142
Submitted by:	Lewis Donzis <lew@perftech.com>
MFC after:	3 days
2019-07-11 16:19:33 +00:00
Konstantin Belousov
2d8c3eeb12 Add libc stub for pthread_getthreadid_np(3).
Requested by:	jbeich
PR:	238650
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2019-06-23 10:50:26 +00:00
Konstantin Belousov
b8f75b175c Do not access mutex memory after unlock.
PR:	237195
Reported by:	freebsd@hurrikhan.eu
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2019-04-12 17:27:19 +00:00
Konstantin Belousov
5d00c5a657 Fix initial exec TLS mode for dynamically loaded shared objects.
If dso uses initial exec TLS mode, rtld tries to allocate TLS in
static space. If there is no space left, the dlopen(3) fails. If space
if allocated, initial content from PT_TLS segment is distributed to
all threads' pcbs, which was missed and caused un-initialized TLS
segment for such dso after dlopen(3).

The mode is auto-detected either due to the relocation used, or if the
DF_STATIC_TLS dynamic flag is set.  In the later case, the TLS segment
is tried to allocate earlier, which increases chance of the dlopen(3)
to succeed.  LLD was recently fixed to properly emit the flag, ld.bdf
did it always.

Initial test by:	dumbbell
Tested by:	emaste (amd64), ian (arm)
Tested by:	Gerald Aryeetey <aryeeteygerald_rogers.com> (arm64)
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D19072
2019-03-29 17:52:57 +00:00
Ed Maste
09b47fc1c2 revert r341429 "disable BIND_NOW in libc, libthr, and rtld"
r345620 by kib@ fixed the rtld issue that caused a crash at startup
during resolution of libc's ifuncs with BIND_NOW.

PR:		233333
Sponsored by:	The FreeBSD Foundation
2019-03-28 02:12:32 +00:00
Mark Johnston
b16150ea90 Have pthread_cond_destroy() return EBUSY if the condvar has waiters.
This is not required of a compliant implementation, but it's easy to
check for and helps improve compatibility with other common
implementations.  Moreover, it's consistent with our
pthread_mutex_destroy().

PR:		234805
Reviewed by:	jhb, kib, ngie
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D19496
2019-03-08 21:07:08 +00:00
Leandro Lupori
9031358d67 silence cast-align warnings from clang on powerpc64
silence the following warning when compiling libthr with clang 8
for powerpc64 architecture:

usr/src/lib/libthr/arch/powerpc/include/pthread_md.h:82:10: error:
cast from 'uint8_t *' (aka 'unsigned char *') to 'struct tcb *'
increases required alignment from 1 to 8 [-Werror,-Wcast-align]
82:  return ((struct tcb *)(_tp - TP_OFFSET));

Submitted by:	alfredo.junior_eldorado.org.br
Reviewed by:	git_bdragon.rtk0.net, emaste, kib, jhibbits, luporl
Differential Revision:	https://reviews.freebsd.org/D18807
2019-02-13 18:28:53 +00:00
Konstantin Belousov
e4314da2e4 Fixes for very early use of the pthread_mutex_* and libthr malloc.
When libthr is statically linked into the binary, order of the
constructors execution is not deterministic.  It is possible for the
application constructor to use pthread_mutex_* functions before the
libthr initialization was done.

Handle it by:
- making thr_malloc.c locking functions operational when curthread is not
  yet set;
- making __thr_malloc_init() idempotent, allowing more than one call to it;
- unconditionally calling __thr_malloc_init() before initializing
  a process-private mutex.

Reported and tested by:	mmel
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2019-02-04 21:16:15 +00:00
Konstantin Belousov
d49ca25de6 Rename rtld-elf/malloc.c to rtld-elf/rtld_malloc.c.
Then malloc.c file name is too generic to use it for libthr.a.

Sponsored by:	The FreeBSD Foundation
MFC after:	13 days
2019-01-30 16:28:27 +00:00
Konstantin Belousov
381c2d2e9a Untangle jemalloc and mutexes initialization.
The need to use libc malloc(3) from some places in libthr always
caused issues.  For instance, per-thread key allocation was switched to
use plain mmap(2) to get storage, because some third party mallocs
used keys for implementation of calloc(3).

Even more important, libthr calls calloc(3) during initialization of
pthread mutexes, and jemalloc uses pthread mutexes.  Jemalloc provides
some way to both postpone the initialization, and to make
initialization to use specialized allocator, but this is very fragile
and often breaks.  See the referenced PR for another example.

Add the small malloc implementation used by rtld, to libthr. Use it in
thr_spec.c and for mutexes initialization. This avoids the issues with
mutual dependencies between malloc and libthr in principle.  The
drawback is that some more allocations are not interceptable for
alternate malloc implementations.  There should be not too much memory
use from this allocator, and the alternative, direct use of mmap(2) is
obviously worse.

PR:	235211
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D18988
2019-01-29 22:46:44 +00:00
Brooks Davis
52af996782 Remove declarations of syscalls not used in libthr.
Reviewed by:	kib
Sponsored by:	DARPA, AFRL
2018-12-03 22:09:23 +00:00
Ed Maste
0e450664ad disable BIND_NOW in libc, libthr, and rtld
An issue remains with BIND_NOW and processes using threads.  For now,
restore libc's BIND_NOW disable, and also disable BIND_NOW in rtld and
libthr.

A patch is in review (D18400) that likely fixes this issue, but just
disable BIND_NOW pending further testing after it is committed.

PR:		233333
Sponsored by:	The FreeBSD Foundation
2018-12-03 15:59:46 +00:00
Eric van Gyzen
dbb1c64e82 Use _thr_isthreaded() and _thr_setthreaded() wrappers
...instead of directly using the global variable.

MFC after:	2 weeks
Sponsored by:	Dell EMC Isilon
2018-11-30 03:02:49 +00:00
Eric van Gyzen
ad8c236b28 _thr_setthreaded() cannot fail; change return type to void
Also remove logic to avoid unnecessary stores to the global variable.
Thread creation and destruction are heavy enough that any supposed savings
is in the noise.

MFC after:	2 weeks
Sponsored by:	Dell EMC Isilon
2018-11-30 03:01:32 +00:00
Edward Tomasz Napierala
a4f02d5d6d Make libthr(3) use sysconf(_SC_NPROCESSORS_CONF); this shaves off
two calls to sysctl(2) from the binary startup.

Reviewed by:	kib
MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D18046
2018-11-19 18:24:08 +00:00
Pedro F. Giffuni
ab82c6bdad libthr: minor spacing cleanup.
No functional change.

X-MFC with:	r337992
2018-08-21 01:33:25 +00:00
Pedro F. Giffuni
b6413b6db8 POSIX compliance improvements in the pthread(3) functions.
This basically adds makes use of the C99 restrict keyword, and also
adds some 'const's to four threading functions: pthread_mutexattr_gettype(),
pthread_mutexattr_getprioceiling(), pthread_mutexattr_getprotocol(), and
pthread_mutex_getprioceiling. The changes are in accordance to POSIX/SUSv4-2018.

Hinted by:	DragonFlyBSD

Relnotes:	yes
MFC after:	1 month
Differential Revision:	D16722
2018-08-18 01:05:38 +00:00
Konstantin Belousov
4627d47bc8 Add pthread_get_name_np(3).
The function retrieves the thread name previously set by
pthread_set_name_np(3). The name is cached in the process memory.

Requested by:	Willem Jan Withagen <wjw@digiware.nl>
Man page update:	Yuri Pankov <yuripv@yuripv.net>
Reviewed by:	ian (previous version)
Discussed with:	arichardson, bjk (man page)
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D16702
2018-08-17 18:34:07 +00:00
Konstantin Belousov
b1bd4736a9 Remove unused file.
The thr_new(2) syscall is called through libc stub.  r154212 stopped
generating it for libthr but did not cleaned the unused remnants.

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2018-06-16 17:11:23 +00:00
John Baldwin
9c4a7c50ae Remove references to the LIBC_SCCS and SYSLIBC_SCCS C macros.
This language dates back to when libthr was libc_r that included its own
syscalls and replaced libc entirely.  It hasn't been relevant for a long
time.
2018-05-23 17:05:12 +00:00
John Baldwin
dc3566067a Ensure thread library is initialized in pthread_testcancel().
Call _thr_check_init() before reading curthread in pthread_testcancel().

If a constructor in a library creates a semaphore via sem_init() and
then waits for it via sem_wait(), the program can core dump in
_pthread_testcancel() called from sem_wait().  This is because the
semaphore implementation lives in libc, so the library's constructors
can be run before libthr's constructors.

Reported by:	arichardson
Reviewed by:	kib
Obtained from:	CheriBSD
MFC after:	1 week
Sponsored by:	DARPA / AFRL
Differential Revision:	https://reviews.freebsd.org/D14786
2018-03-21 21:13:26 +00:00
Pedro F. Giffuni
b9b20abf2b libthr: Fix missing break in switch.
This is also a warning in recent GCC with -Wimplicit-fallthrough.

CID:	1356262
Obtained from:	DragonFly (git 0f037c78 - partial)
2018-01-27 22:27:55 +00:00
Pedro F. Giffuni
5e53a4f90f lib: further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 2-Clause license, however the tool I
was using mis-identified many licenses so this was mostly a manual - error
prone - task.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.
2017-11-26 02:00:33 +00:00
Pedro F. Giffuni
8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Pedro F. Giffuni
df57947f08 spdx: initial adoption of licensing ID tags.
The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.

Initially, only tag files that use BSD 4-Clause "Original" license.

RelNotes:	yes
Differential Revision:	https://reviews.freebsd.org/D13133
2017-11-18 14:26:50 +00:00
Warner Losh
25dfe63ccb Use MACHINE_CPUARCH in preference MACHINE for userland.
Sponsored by: Netflix
2017-11-07 09:57:26 +00:00
Bryan Drewery
ea825d0274 DIRDEPS_BUILD: Update dependencies.
Sponsored by:	Dell EMC Isilon
2017-10-31 00:07:04 +00:00
Michal Meloun
f879eb0488 Fix misleading comment.
Not a functional change.

MFC after:	3 days
2017-10-29 14:26:37 +00:00
Michael Zhilin
37a6f46190 [libthr] revert change of visibility of _thread_keytable to unbreak debugger
Fix regression by r318539. The sysutils/pstack uses library libthread_db to
read information about threads state. The function pt_ta_new makes lookup of
several key symbols including _thread_keytable. But r318539 mades this field
static. It causes silent ignore of libthr library by pstack and as result
sysutils/pstack doesn't output any thread information.

This fix changes this field back to non-static.

MFC after:	2 weeks
Differential Revision:	https://reviews.freebsd.org/D11738
2017-10-02 20:33:16 +00:00
Enji Cooper
d511b20a69 Add HAS_TESTS to all Makefiles that are currently using the
`SUBDIR.${MK_TESTS}+= tests` idiom.

This is a follow up to r321912.
2017-08-02 08:50:42 +00:00
Enji Cooper
4b330699f8 Convert traditional ${MK_TESTS} conditional idiom for including test
directories to SUBDIR.${MK_TESTS} idiom

This is being done to pave the way for future work (and homogenity) in
^/projects/make-check-sandbox .

No functional change intended.

MFC after:	1 weeks
2017-08-02 08:35:51 +00:00
Pedro F. Giffuni
1c9158aabe libthr: check for possible overflow in the pthread_barrier_init() count.
Following up on r320900, where the check for negative count values was
removed, add a check to prevent integer overflow. This is to account that
b_count, b_waiters but most importantly the total number of threads in
the system are signed values.

Discussed with:	kib
MFC after:	2 weeks
2017-07-15 15:00:13 +00:00
Pedro F. Giffuni
97c3a994e4 libthr: 'count' is unsigned so don't check for negative values.
Obtained from:	DragonFlyBSD (git e7db8139)
2017-07-14 16:05:54 +00:00
Eric van Gyzen
afba14e2b8 libthr: increase WARNS to the default (6)
...and silence cast-align warnings from gcc.

Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10935
2017-05-26 15:57:54 +00:00
Eric van Gyzen
718fb5ba5b libthr: fix warnings at WARNS=6
Fix more warnings about redundant declarations.

Reviewed by:	kib emaste
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10932
2017-05-26 15:56:28 +00:00
Eric van Gyzen
01618b339f libthr: fix style in previous commit
I intended to add this to the previous commit.

Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
2017-05-26 15:53:27 +00:00
Eric van Gyzen
d25183e0a7 libthr: prevent setcontext() from masking SIGTHR
__thr_setcontext() mistakenly tested for the presence of SIGCANCEL
in its local ucontext_t instead of the parameter. Therefore,
if a thread calls setcontext() with a context whose signal mask
contains SIGTHR (a.k.a. SIGCANCEL), that signal will be blocked,
preventing the thread from being cancelled or suspended.

Reported by:	gcc 6.1 via RISC-V tinderbox
Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10933
2017-05-26 15:51:51 +00:00
Eric van Gyzen
5a6d7b723f libthr: fix warnings from GCC when WARNS=6
Fix warnings about:
- redundant declarations
- a local variable shadowing a global function (dlinfo)
- an old-style function definition (with an empty parameter list)
- a variable that is possibly used uninitialized

"make tinderbox" passes this time, except for a few unrelated
kernel failures.

Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10870
2017-05-23 16:12:50 +00:00
Eric van Gyzen
cfc1010f6d Revert r318583 (libthr: use default WARNS level of 6)
Revert this while I fix RISC-V, SPARC, and probably all architectures
that use GCC.

Reported by:	kib
Pointy hat to:	vangyzen
Sponsored by:	Dell EMC
2017-05-22 13:21:28 +00:00
Eric van Gyzen
0b2f3f2058 libthr: Use CLI flags instead of pragmas to disable warnings
People tweaking the build system or compilers tend to look into
the Makefile and not into the source.  Having some warning controls
in the Makefile and some in the source code is surprising.

Pragmas have the advantage that they leave the warnings enabled
for more code, but that advantage isn't very relevant in these cases.

Requested by:	kib
Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10832
2017-05-20 17:33:47 +00:00
Eric van Gyzen
d71b289766 libthr: use default WARNS level of 6
Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10832
2017-05-20 17:32:30 +00:00
Eric van Gyzen
07f29d9f76 Remove old spinlock_debug code from libc
This no longer seems useful.  Remove it.

This was prompted by a "cast discards volatile qualifier" warning
in libthr when WARNS=6.

Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10832
2017-05-20 17:32:01 +00:00
Eric van Gyzen
048ad6aedc libthr: change CHECK_AND_INIT_RWLOCK to an inline function
This was prompted by a compiler warning about 'ret' shadowing
a local variable in the callers of the macro.

Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10832
2017-05-20 17:30:48 +00:00
Eric van Gyzen
6eccf6e38f libthr: disable thread-safety warnings
These warnings don't make sense for code that implements
the locking primitives.

Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10832
2017-05-20 17:29:36 +00:00
Eric van Gyzen
d2335a57f4 libthr: fix warnings at WARNS=6
Fix warnings about the following when WARNS=6 (which I will commit soon):

- casting away const
- no previous 'extern' declaration for non-static variable
- others as explained by #pragmas and comments
- unused parameters

The last is the only functional change.

Reviewed by:	kib
MFC after:	3 days
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10808
2017-05-19 13:04:05 +00:00
Jason Evans
c45e7190ab Fix __pthread_mutex_trylock() to call THR_CRITICAL_LEAVE() on failure rather
than on success.  This regression was introduced by r300043 (Add implementation
of robust mutexes...).

MFC after:	1 day
2017-05-13 17:49:53 +00:00
Eric van Gyzen
3f8455b090 Add clock_nanosleep()
Add a clock_nanosleep() syscall, as specified by POSIX.
Make nanosleep() a wrapper around it.

Attach the clock_nanosleep test from NetBSD. Adjust it for the
FreeBSD behavior of updating rmtp only when interrupted by a signal.
I believe this to be POSIX-compliant, since POSIX mentions the rmtp
parameter only in the paragraph about EINTR. This is also what
Linux does. (NetBSD updates rmtp unconditionally.)

Copy the whole nanosleep.2 man page from NetBSD because it is complete
and closely resembles the POSIX description. Edit, polish, and reword it
a bit, being sure to keep any relevant text from the FreeBSD page.

Reviewed by:	kib, ngie, jilles
MFC after:	3 weeks
Relnotes:	yes
Sponsored by:	Dell EMC
Differential Revision:	https://reviews.freebsd.org/D10020
2017-03-19 00:51:12 +00:00
Pedro F. Giffuni
649702c5a3 Make use of clang nullability attributes.
Replace uses of the GCC __nonnull__ attribute with the clang nullability
qualifiers. The replacement should be transparent for clang developers as
the new qualifiers will produce the same warnings and will be useful for
static checkers but will not cause aggressive optimizations.

GCC will not produce such warnings and developers will have to use
upgraded GCC ports built with the system headers from r312538.

Hinted by:	Apple's Libc-1158.20.4, Bionic libc
MFC after:	11.1 Release

Differential Revision:	https://reviews.freebsd.org/D9004
2017-01-28 20:54:43 +00:00
Ed Maste
2d0e733b3a libthr: coalesce repeated #if blocks 2017-01-25 20:19:48 +00:00
Enji Cooper
58368ba984 Use SRCTOP-relative paths to other directories instead of .CURDIR-relative ones
This implifies pathing in make/displayed output

MFC after:    3 weeks
Sponsored by: Dell EMC Isilon
2017-01-20 04:32:19 +00:00
Enji Cooper
cdebaff820 Upgrade NetBSD tests to 01.11.2017_23.20 snapshot
This contains some new testcases in /usr/tests/...:

- .../lib/libc
- .../lib/libthr
- .../lib/msun
- .../sys/kern

Tested on:	amd64, i386
MFC after:	1 month
2017-01-13 03:33:57 +00:00
Konstantin Belousov
4dafad49c6 Do not leak curthread->inact_mtx when cancelling in pthread_cond_wait(3).
Leave robust-protected region before checking for cancellation by
calling _thr_testcancel().  Otherwise, if cancelling request was
pending, the cancel handler is called with the dandling inact_mtx,
which triggers an assert if any mutex operation is performed by the
handler.

Reported and tested by:	Dimitri Staessens <dimitri.staessens@intec.ugent.be>
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2016-12-06 17:13:17 +00:00
Marcel Moolenaar
2b4da8aa20 When MAKEOBJDIRPREFIX points to a case-insensitive file system, the
build can break when different source files create the same target
files (case-insensitivity speaking).  This is the case for object
files compiled with -fpic and shared libraries. The former uses
an extension of ".So", and the latter an extension ".so".  Rename
shared object files from *.So to *.pico to match what NetBSD does.

See also r305855

MFC after:	1 month
Sponsored by:	Bracket Computing
Differential Revision:	https://reviews.freebsd.org/D7906
2016-09-24 15:11:27 +00:00
Konstantin Belousov
1c1cc89580 The fdatasync(2) call must be cancellation point.
Sponsored by:	The FreeBSD Foundation
MFC after:	13 days
2016-08-16 08:27:03 +00:00
Adrian Chadd
cb916ce754 [mips] convert over =v to =r for the inline assembly.
Later gcc and clang have deprecated =v (which maps to a specific temp
register) and instead we should just use =r to have the assembler
(hopefully!) save/restore things appropriately after choosing
a register.

Tested:

* AR9344 SoC, with userreg support
* AR9331 SoC, with no userreg support

Sponsored by:	Sponsored by: DARPA, AFRL (MIPS TLS user register work)
2016-08-14 19:04:37 +00:00
Adrian Chadd
b812fe4d6b [mips] add support for using the MIPS user register for TLS data.
This work, originally from Stacey Son, uses the MIPS UserReg for
reading the TLS data, and will fall back to the normal syscall path
when it isn't supported.

This code dynamically patches cpu_switch() to bypass the UserReg
instruction so to avoid generating a machine exception.

Thanks to sson for the original work, and to Dan Nelson for
bringing it to date and testing it on MIPS32 with me.

Tested:

* mips64 (sson)
* mips74k (dnelson_1901@yahoo.com) - AR9344 SoC, UserReg support
* mips24k (adrian) - AR9331 SoC, no UserReg support

Obtained from:	sson, dnelson_1901@yahoo.com
2016-08-07 01:29:55 +00:00
Konstantin Belousov
b585cd3e2c Add __cxa_thread_atexit(3) API implementation.
This is the backing feature to implement C++11 thread storage duration
specified by the thread_local keyword.  A destructor for given
thread-local object is registered to be executed at the thread
termination time using __cxa_thread_atexit().  Libc calls the
__cxa_thread_calls_dtors() during exit(3), before finalizers and
atexit functions, and libthr calls the function at the thread
termination time, after the stack unwinding and thread-specific key
destruction.

There are several uncertainties in the API which lacks a formal
specification.  Among them:
- is it allowed to register destructors during destructing;
	we allow, but limiting the nesting level.  If too many iterations
	detected, a diagnostic is issued to stderr and thread forcibly
	terminates for now.
- how to handle destructors which belong to an unloading dso;
	for now, we ignore destructor calls for such entries, and
	issue a diagnostic.  Linux does prevent dso unload until all
	threads with destructors from the dso terminated.
It is supposed that the diagnostics allow to detect real-world
applications relying on the above details and possibly adjust
our implementation.  Right now the choices were to provide the slim
API (but that rarely stands the practice test).

Tests are added to check generic functionality and to specify some of
the above implementation choices.

Submitted by:	Mahdi Mokhtari <mokhi64_gmail.com>
Reviewed by:	theraven
Discussed with:	dim (detection of -std=c++11 supoort for tests)
Sponsored by:	The FreeBSD Foundation (my involvement)
MFC after:	2 weeks
Differential revisions:	https://reviews.freebsd.org/D7224,
    https://reviews.freebsd.org/D7427
2016-08-06 13:32:40 +00:00
Konstantin Belousov
bf890e4877 Remove empty initializer for the once facility. It was not needed
since r179417.

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2016-07-27 15:14:11 +00:00
Konstantin Belousov
4edfc1e3bf Revert r302194, there are issues with some applications after changing
the return value, in particular console-kit-daemon.

Reported by:	Ivan Klymenko <fidaj@ukr.net>
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Approved by:	re (gjb)
2016-06-25 20:20:24 +00:00
Konstantin Belousov
a068480247 For pthread_mutex_trylock() call on owned error-check or non-portable
adaptive mutex, return EDEADLK as required by POSIX.  The
pthread_mutex_lock() is already compliant.

Tested by:	Guy Yur <guyyur@gmail.com>
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Approved by:	re (gjb)
2016-06-25 11:30:40 +00:00
Konstantin Belousov
7d6906b804 Fix markup.
Sponsored by:	The FreeBSD Foundation
2016-06-04 20:20:14 +00:00
Conrad Meyer
c72ef5eab3 libthr: Use formatted PANIC()
No functional change, although _thread_printf() may be slightly less functional
or render some values differently from libc snprintf(3).  No ABI change.

Reviewed by:	kib
Sponsored by:	EMC / Isilon Storage Division
Differential Revision:	https://reviews.freebsd.org/D6672
2016-06-01 16:12:26 +00:00
Conrad Meyer
a3c0056121 libthr: _thread_vprintf: Enhance support for %p, %#x
No functional change.  No ABI change.

Reviewed by:	kib
Sponsored by:	EMC / Isilon Storage Division
Differential Revision:	https://reviews.freebsd.org/D6672
2016-06-01 16:11:09 +00:00
Conrad Meyer
3a7d122f96 libthr: Add vprintf variant of _thread_printf, formatted PANIC()
No ABI change.

Reviewed by:	kib
Sponsored by:	EMC / Isilon Storage Division
Differential Revision:	https://reviews.freebsd.org/D6672
2016-06-01 16:09:56 +00:00
Konstantin Belousov
6180f50bbb Stop inlining the struct ucond definition into struct pthread_cond.
This avoids unneccessary casts and make the calls to _thr_ucond_*()
functions less questionable.

The c_spare field was not included into struct pthread_cond, so the
change modifies libthr ABI for shared condvars.  But since an off-page
does not legitimately contains any other data past the struct
pthread_cond, the change keeps shared condvars from pre- and post-
changed libthr compatible.  Also note that the whole struct ucond was
never copied in or out by kernel.

For private condvars, the privately allocated memory was never exposed
outside libthr.

Sponsored by:	The FreeBSD Foundation
2016-05-29 19:35:55 +00:00
Jilles Tjoelker
60404ec965 Remove non-history libkse references and fix PTHREAD_PROCESSES_PRIVATE typo. 2016-05-29 18:25:11 +00:00
Konstantin Belousov
41ea34a25c Document _umtx_op(2) interface for the implementation of robust mutexes.
In libthr(3), list added knobs.

Reviewed by:	emaste
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D6427
2016-05-19 17:40:00 +00:00
Konstantin Belousov
2a339d9e3d Add implementation of robust mutexes, hopefully close enough to the
intention of the POSIX IEEE Std 1003.1TM-2008/Cor 1-2013.

A robust mutex is guaranteed to be cleared by the system upon either
thread or process owner termination while the mutex is held.  The next
mutex locker is then notified about inconsistent mutex state and can
execute (or abandon) corrective actions.

The patch mostly consists of small changes here and there, adding
neccessary checks for the inconsistent and abandoned conditions into
existing paths.  Additionally, the thread exit handler was extended to
iterate over the userspace-maintained list of owned robust mutexes,
unlocking and marking as terminated each of them.

The list of owned robust mutexes cannot be maintained atomically
synchronous with the mutex lock state (it is possible in kernel, but
is too expensive).  Instead, for the duration of lock or unlock
operation, the current mutex is remembered in a special slot that is
also checked by the kernel at thread termination.

Kernel must be aware about the per-thread location of the heads of
robust mutex lists and the current active mutex slot.  When a thread
touches a robust mutex for the first time, a new umtx op syscall is
issued which informs about location of lists heads.

The umtx sleep queues for PP and PI mutexes are split between
non-robust and robust.

Somewhat unrelated changes in the patch:
1. Style.
2. The fix for proper tdfind() call use in umtxq_sleep_pi() for shared
   pi mutexes.
3. Removal of the userspace struct pthread_mutex m_owner field.
4. The sysctl kern.ipc.umtx_vnode_persistent is added, which controls
   the lifetime of the shared mutex associated with a vnode' page.

Reviewed by:	jilles (previous version, supposedly the objection was fixed)
Discussed with:	brooks, Martin Simmons <martin@lispworks.com> (some aspects)
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
2016-05-17 09:56:22 +00:00
Jilles Tjoelker
4992013f8f libthr(3): Fix xref to _umtx_op(2) now that we have it. 2016-05-16 13:39:04 +00:00
Konstantin Belousov
321e2a0090 Do not leak THR_FLAGS_SUSPENDED from the previous suspend/resume
cycle.  The flag currently is cleared by the resumed thread.  If next
suspend request comes before the thread was able to clean the flag, in
which case suspender skip the thread.

Instead, clear the THR_FLAGS_SUSPEND flag in resume_common(), we do
not care how much code was executed in the resumed thread when the
pthread_resume_*np(s) functions returned.

PR:	209233
Reported by:	Lawrence Esswood <le277@cam.ac.uk>
MFC after:	1 week
2016-05-05 10:20:22 +00:00
Enji Cooper
430f7286a5 Merge ^/user/ngie/release-pkg-fix-tests to unbreak how test files are installed
after r298107

Summary of changes:

- Replace all instances of FILES/TESTS with ${PACKAGE}FILES. This ensures that
  namespacing is kept with FILES appropriately, and that this shouldn't need
  to be repeated if the namespace changes -- only the definition of PACKAGE
  needs to be changed
- Allow PACKAGE to be overridden by callers instead of forcing it to always be
  `tests`. In the event we get to the point where things can be split up
  enough in the base system, it would make more sense to group the tests
  with the blocks they're a part of, e.g. byacc with byacc-tests, etc
- Remove PACKAGE definitions where possible, i.e. where FILES wasn't used
  previously.
- Remove unnecessary TESTSPACKAGE definitions; this has been elided into
  bsd.tests.mk
- Remove unnecessary BINDIRs used previously with ${PACKAGE}FILES;
  ${PACKAGE}FILESDIR is now automatically defined in bsd.test.mk.
- Fix installation of files under data/ subdirectories in lib/libc/tests/hash
  and lib/libc/tests/net/getaddrinfo
- Remove unnecessary .include <bsd.own.mk>s (some opportunistic cleanup)

Document the proposed changes in share/examples/tests/tests/... via examples
so it's clear that ${PACKAGES}FILES is the suggested way forward in terms of
replacing FILES. share/mk/bsd.README didn't seem like the appropriate method
of communicating that info.

MFC after: never probably
X-MFC with: r298107
PR: 209114
Relnotes: yes
Tested with: buildworld, installworld, checkworld; buildworld, packageworld
Sponsored by: EMC / Isilon Storage Division
2016-05-04 23:20:53 +00:00
Glen Barber
0edd2576c0 MFH
Sponsored by:	The FreeBSD Foundation
2016-04-16 02:32:12 +00:00
Ruslan Bukin
5be589603e Unmagic the thread pointer offset. 2016-04-14 15:31:05 +00:00
Glen Barber
a123f26e92 MFH
Sponsored by:	The FreeBSD Foundation
2016-04-12 17:00:13 +00:00
Konstantin Belousov
fe60c14631 If off-page lookup failed, there is no memory to perform
shared_mutex_init() upon.

Sponsored by:	The FreeBSD Foundation
2016-04-12 10:25:44 +00:00
Glen Barber
876d357fa7 MFH
Sponsored by:	The FreeBSD Foundation
2016-04-11 15:24:59 +00:00
Konstantin Belousov
3279301186 Use __FBSDID() for .c files from lib/libthr/thread.
Sponsored by:	The FreeBSD Foundation
2016-04-08 11:15:26 +00:00
Konstantin Belousov
5c43c9a105 Use ANSI C function definitions, fix spelling in a comment.
Sponsored by:	The FreeBSD Foundation
2016-04-08 10:59:06 +00:00
Konstantin Belousov
9e821f2796 Assert that the lock objects put into the off-page, fit into the page.
Sponsored by:	The FreeBSD Foundation
2016-04-08 10:21:43 +00:00
Glen Barber
d60840138f MFH
Sponsored by:	The FreeBSD Foundation
2016-04-04 23:55:32 +00:00
Konstantin Belousov
841ecd471a Remove unused variable. It was write-only before r297139.
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2016-04-04 06:58:59 +00:00
Konstantin Belousov
6044c03a37 Apparently there are some popular programs around which assume that it
is safe to call pthread_mutex_init() on the same shared mutex several
times.  POSIX claims that the behaviour in this case is undefined.

Make this working by only allowing one caller to initialize the mutex.
Other callers either see already completed initialization and do
nothing, or busy-loop yielding while designated initializer finishes.
Also make the API requirements loose by initializing mutexes on other
pthread_mutex*() calls if they see uninitialized shared mutex.

Only mutexes provide the hack for now, but it could be also
implemented for other process shared primitives from libthr.

Reported and tested by:	"Oleg V. Nauman" <oleg@opentransfer.com>
Sponsored by:	The FreeBSD Foundation
2016-03-22 10:51:42 +00:00
Konstantin Belousov
53fd961f05 Lock pshared_lock shared around fork, to ensure that the COW snapshot
of the pshared hash in child is consistent and can be safely used.

Reported and tested by:	"Oleg V. Nauman" <oleg@opentransfer.com>
Sponsored by:	The FreeBSD Foundation
2016-03-21 06:52:35 +00:00
Konstantin Belousov
07f22a288d Provide more information on failing checks in mutex_assert_is_owned()
and mutex_assert_not_owned().  snprintf() use in this context should
be safe.

Sponsored by:	The FreeBSD Foundation
2016-03-21 06:48:11 +00:00
Konstantin Belousov
b6751c3f87 From libthr, remove special and strange code to set up session and
control terminal, activated when running with pid 1.  It is
application duty to handle this, and unsuspecting init replacements
which are linked with libthr would be broken by this.

The pre-resolving of getpid() is restored, just in case.

Reviewed by:	jilles
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
2016-03-21 06:46:16 +00:00
Konstantin Belousov
6eced731e8 Implement process-shared spinlocks.
Sponsored by:	The FreeBSD Foundation
2016-03-21 06:40:54 +00:00
Konstantin Belousov
e13c604596 Fix typo.
MFC after:	3 days
2016-03-21 05:59:05 +00:00
Glen Barber
7d536dc855 MFH
Sponsored by:	The FreeBSD Foundation
2016-03-10 21:16:01 +00:00
Bryan Drewery
15c433351f DIRDEPS_BUILD: Connect MK_TESTS.
Sponsored by:	EMC / Isilon Storage Division
2016-03-09 22:46:01 +00:00
Glen Barber
52259a98ad MFH
Sponsored by:	The FreeBSD Foundation
2016-03-02 16:14:46 +00:00
Konstantin Belousov
064094126c Add two comments explaining the fine points of the hash
implementation.

Reviewed by:	emaste
Sponsored by:	The FreeBSD Foundation
Differential revision:	https://reviews.freebsd.org/D5490
2016-03-01 15:21:01 +00:00
Edward Tomasz Napierala
be9430ef05 errno(3) -> errno(2)
MFC after:	1 month
Sponsored by:	The FreeBSD Foundation
2016-02-29 17:34:54 +00:00
Konstantin Belousov
1bdbd70599 Implement process-shared locks support for libthr.so.3, without
breaking the ABI.  Special value is stored in the lock pointer to
indicate shared lock, and offline page in the shared memory is
allocated to store the actual lock.

Reviewed by:	vangyzen (previous version)
Discussed with:	deischen, emaste, jhb, rwatson,
	Martin Simmons <martin@lispworks.com>
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
2016-02-28 17:52:33 +00:00
Glen Barber
acbbf33c47 MFH
Sponsored by:	The FreeBSD Foundation
2016-02-09 01:42:51 +00:00
Konstantin Belousov
bd43f0691c If libthr.so is dlopened without RTLD_GLOBAL flag, the libthr symbols
do not participate in the global symbols namespace, but rtld locks are
still replaced and functions are interposed.  In particular,
__pthread_map_stacks_exec is resolved to the libc version.  If a
library is loaded later, which requires adjustment of the stack
protection mode, rtld calls into libc __pthread_map_stacks_exec due to
the symbols scope.  The libc version might recurse into binder and
recursively acquire rtld bind lock, causing the hang.

Make libc __pthread_map_stacks_exec() interposed, which synchronizes
rtld locks and version of the stack exec hook when libthr loaded,
regardless of the symbol scope control or symbol resolution order.

The __pthread_map_stacks_exec() symbol is removed from the private
version in libthr since libc symbol now operates correctly in presence
of libthr.

Reported and tested by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
2016-02-08 19:24:13 +00:00
Glen Barber
c389411c8a Remove libc, librtld_db, libthr packages, and further increase
the constraints on what needs to be installed in a specific to
maintain consistency during upgrades.

Create a new clibs package containing libraries that are needed
as a bare minimum for consistency.

With much help and input from:	kib
Sponsored by:	The FreeBSD Foundation
2016-02-05 21:01:08 +00:00
Glen Barber
a70cba9582 First pass through library packaging.
Sponsored by:	The FreeBSD Foundation
2016-02-04 21:16:35 +00:00