Commit Graph

48 Commits

Author SHA1 Message Date
kib eb82d7086c Implement address space guards.
Guard, requested by the MAP_GUARD mmap(2) flag, prevents the reuse of
the allocated address space, but does not allow instantiation of the
pages in the range.  It is useful for more explicit support for usual
two-stage reserve then commit allocators, since it prevents accidental
instantiation of the mapping, e.g. by mprotect(2).

Use guards to reimplement stack grow code.  Explicitely track stack
grow area with the guard, including the stack guard page.  On stack
grow, trivial shift of the guard map entry and stack map entry limits
makes the stack expansion.  Move the code to detect stack grow and
call vm_map_growstack(), from vm_fault() into vm_map_lookup().

As result, it is impossible to get random mapping to occur in the
stack grow area, or to overlap the stack guard page.

Enable stack guard page by default.

Reviewed by:	alc, markj
Man page update reviewed by:	alc, bjk, emaste, markj, pho
Tested by:	pho, Qualys
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D11306 (man pages)
2018-08-24 15:00:02 +02:00
glebius 99b9b925fe Listening sockets improvements.
o Separate fields of struct socket that belong to listening from
  fields that belong to normal dataflow, and unionize them.  This
  shrinks the structure a bit.
  - Take out selinfo's from the socket buffers into the socket. The
    first reason is to support braindamaged scenario when a socket is
    added to kevent(2) and then listen(2) is cast on it. The second
    reason is that there is future plan to make socket buffers pluggable,
    so that for a dataflow socket a socket buffer can be changed, and
    in this case we also want to keep same selinfos through the lifetime
    of a socket.
  - Remove struct struct so_accf. Since now listening stuff no longer
    affects struct socket size, just move its fields into listening part
    of the union.
  - Provide sol_upcall field and enforce that so_upcall_set() may be called
    only on a dataflow socket, which has buffers, and for listening sockets
    provide solisten_upcall_set().

o Remove ACCEPT_LOCK() global.
  - Add a mutex to socket, to be used instead of socket buffer lock to lock
    fields of struct socket that don't belong to a socket buffer.
  - Allow to acquire two socket locks, but the first one must belong to a
    listening socket.
  - Make soref()/sorele() to use atomic(9).  This allows in some situations
    to do soref() without owning socket lock.  There is place for improvement
    here, it is possible to make sorele() also to lock optionally.
  - Most protocols aren't touched by this change, except UNIX local sockets.
    See below for more information.

o Reduce copy-and-paste in kernel modules that accept connections from
  listening sockets: provide function solisten_dequeue(), and use it in
  the following modules: ctl(4), iscsi(4), ng_btsocket(4), ng_ksocket(4),
  infiniband, rpc.

o UNIX local sockets.
  - Removal of ACCEPT_LOCK() global uncovered several races in the UNIX
    local sockets.  Most races exist around spawning a new socket, when we
    are connecting to a local listening socket.  To cover them, we need to
    hold locks on both PCBs when spawning a third one.  This means holding
    them across sonewconn().  This creates a LOR between pcb locks and
    unp_list_lock.
  - To fix the new LOR, abandon the global unp_list_lock in favor of global
    unp_link_lock.  Indeed, separating these two locks didn't provide us any
    extra parralelism in the UNIX sockets.
  - Now call into uipc_attach() may happen with unp_link_lock hold if, we
    are accepting, or without unp_link_lock in case if we are just creating
    a socket.
  - Another problem in UNIX sockets is that uipc_close() basicly did nothing
    for a listening socket.  The vnode remained opened for connections.  This
    is fixed by removing vnode in uipc_close().  Maybe the right way would be
    to do it for all sockets (not only listening), simply move the vnode
    teardown from uipc_detach() to uipc_close()?

Sponsored by:		Netflix
Differential Revision:	https://reviews.freebsd.org/D9770
2018-08-24 15:00:02 +02:00
delphij ca3b7a988a Implement INHERIT_ZERO for minherit(2).
INHERIT_ZERO is an OpenBSD feature.

When a page is marked as such, it would be zeroed
upon fork().

This would be used in new arc4random(3) functions.

PR:	182610
Reviewed by:	kib (earlier version)
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D427
2018-08-24 15:00:02 +02:00
imp 16636ede3c Renumber copyright clause 4
Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by:	Jan Schaumann <jschauma@stevens.edu>
Pull Request:	https://github.com/freebsd/freebsd/pull/96
2018-08-24 15:00:02 +02:00
ed@FreeBSD.org 08139e557b mprotect(): Change prototype to comply to POSIX.
Our mprotect() function seems to take a "const void *" address to the
pages whose permissions need to be adjusted. POSIX uses "void *". Simply
stick to the POSIX one to prevent us from writing unportable code.

PR:		211423 (exp-run)
Tested by:	antoine@ (Thanks!)
2018-08-24 15:00:02 +02:00
kib c3df6d5155 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
2018-08-24 15:00:02 +02:00
jhb 7cfc736e89 Add a new file operations hook for mmap
operations. File type-specific logic is now placed in the mmap hook
implementation rather than requiring it to be placed in
sys/vm/vm_mmap.c. This hook allows new file types to support mmap() as
well as potentially allowing mmap() for existing file types that do not
currently support any mapping.

The vm_mmap() function is now split up into two functions.  A new
vm_mmap_object() function handles the "back half" of vm_mmap() and accepts
a referenced VM object to map rather than a (handle, handle_type) tuple.
vm_mmap() is now reduced to converting a (handle, handle_type) tuple to a
a VM object and then calling vm_mmap_object() to handle the actual mapping.
The vm_mmap() function remains for use by other parts of the kernel
(e.g. device drivers and exec) but now only supports mapping vnodes,
character devices, and anonymous memory.

The mmap() system call invokes vm_mmap_object() directly with a NULL object
for anonymous mappings.  For mappings using a file descriptor, the
descriptors fo_mmap() hook is invoked instead.  The fo_mmap() hook is
responsible for performing type-specific checks and adjustments to
arguments as well as possibly modifying mapping parameters such as flags
or the object offset.  The fo_mmap() hook routines then call
vm_mmap_object() to handle the actual mapping.

The fo_mmap() hook is optional.  If it is not set, then fo_mmap() will
fail with ENODEV.  A fo_mmap() hook is implemented for regular files,
character devices, and shared memory objects (created via shm_open()).

While here, consistently use the VM_PROT_* constants for the vm_prot_t
type for the 'prot' variable passed to vm_mmap() and vm_mmap_object()
as well as the vm_mmap_vnode() and vm_mmap_cdev() helper routines.
Previously some places were using the mmap()-specific PROT_* constants
instead.  While this happens to work because PROT_xx == VM_PROT_xx,
using VM_PROT_* is more correct.

Differential Revision:	https://reviews.freebsd.org/D2658
Reviewed by:	alc (glanced over), kib
MFC after:	1 month
Sponsored by:	Chelsio
2018-08-24 15:00:02 +02:00
jhb 60b466fbc2 Retire the unimplemented MAP_RENAME
and MAP_NORESERVE flags to mmap(2). Older binaries are still permitted
to use these flags.

PR:		193961 (exp-run in ports)
Differential Revision:	https://reviews.freebsd.org/D848
Reviewed by:	kib
2018-08-24 15:00:02 +02:00
jhb 3d5043e2cb Add a new fo_fill_kinfo fileops method
to add type-specific information to struct kinfo_file. - Move the
various fill_*_info() methods out of kern_descrip.c and into the various
file type implementations. - Rework the support for kinfo_ofile to
generate a suitable kinfo_file object for each file and then convert
that to a kinfo_ofile structure rather than keeping a second, different
set of code that directly manipulates type-specific file information. -
Remove the shm_path() and ksem_info() layering violations.

Differential Revision:	https://reviews.freebsd.org/D775
Reviewed by:	kib, glebius (earlier version)
2018-08-24 15:00:02 +02:00
kib de24ef326d Add MAP_EXCL flag for mmap(2).
It should be combined with MAP_FIXED, and prevents the request from
deleting existing mappings in the region, failing instead.

Reviewed by:	alc
Discussed with:	jhb
Tested by:	markj, pho (previous version, as part of the bigger patch)
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2018-08-24 15:00:02 +02:00
jhb 472476a5a7 Add a mmap flag (MAP_32BIT) on 64-bit platforms
to request that a mapping use an address in the first 2GB of the
process's address space. This flag should have the same semantics as the
same flag on Linux.

To facilitate this, add a new parameter to vm_map_find() that specifies an
optional maximum virtual address.  While here, fix several callers of
vm_map_find() to use a VMFS_* constant for the findspace argument instead of
TRUE and FALSE.

Reviewed by:	alc
Approved by:	re (kib)
2018-08-24 15:00:02 +02:00
kib e6a85661ce Implement read(2)/write(2) and neccessary lseek(2)
for posix shmfd. Add MAC framework entries for posix shm read and write.

Do not allow implicit extension of the underlying memory segment past
the limit set by ftruncate(2) by either of the syscalls.  Read and
write returns short i/o, lseek(2) fails with EINVAL when resulting
offset does not fit into the limit.

Discussed with:	alc
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
2018-08-24 15:00:02 +02:00
Joel Sherrill 5b97e36239 rtems/.../dirent.h: Add alphasort() prototype 2018-03-13 09:11:47 -05:00
Sebastian Huber dadc9e7e4a RTEMS: Add semaphore <sys/lock.h> functions
Declare semaphore try wait and post binary functions.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-11-30 07:00:45 +01:00
Sebastian Huber 5a2ab9d55e RTEMS: Timed wait by ticks <sys/lock.h> functions
Declare timed wait by ticks functions.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-11-30 07:00:45 +01:00
Sebastian Huber 186166f67a RTEMS: Add set/get name <sys/lock.h> functions
Add inline functions to set/get the name.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-11-30 07:00:45 +01:00
Sebastian Huber c165a27c01 RTEMS: Fix _PTHREAD_MUTEX_INITIALIZER
Add missing braces around initializer.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-10-13 08:07:13 +02:00
Sebastian Huber 3a79700c2d RTEMS: Make pthread_mutex_t self-contained
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-10-05 14:56:13 +02:00
Sebastian Huber 55c5dda9b5 RTEMS: Make pthread_cond_t self-contained
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-10-05 14:56:13 +02:00
Sebastian Huber d902eef093 RTEMS: Make pthread_rwlock_t self-contained
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-10-05 14:56:13 +02:00
Sebastian Huber 9187bb23a0 RTEMS: Make pthread_barrier_t self-contained
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-10-05 14:56:13 +02:00
Sebastian Huber 4fef7312b3 RTEMS: Optimize pthread_once_t
Reduce size of pthread_once_t and make it zero-initialized.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-10-05 14:56:12 +02:00
Sebastian Huber a3617d7bde Synchronize RTEMS <sys/bitset.h> with FreeBSD
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-07-12 08:04:45 +02:00
Sebastian Huber 1d49ce4d6b Synchronize RTEMS <sys/bitset.h> with FreeBSD
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-07-10 07:18:10 +02:00
Sebastian Huber e9085e0ccd Fix RTEMS ioctl() declaration
Using uint32_t for ioctl_command_t does not work well on 64-bit targets.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-06-07 15:46:20 +02:00
Sebastian Huber dc93d7adff Update FreeBSD revision of RTEMS <sys/bitset.h>
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-06-07 15:46:20 +02:00
Sebastian Huber fd5d052d40 Fix some RTEMS CPUSET(9) macros
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-06-07 15:46:19 +02:00
Sebastian Huber c29f5b219d Fix RTEMS CPU_EQUAL_S()
According to the FreeBSD man page BIT_CMP() returns true in case the two
sets are NOT equal.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-06-07 15:46:19 +02:00
Sebastian Huber d70983ea1b Add de-facto standard <sys/ioctl.h> for RTEMS
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-05-25 12:42:08 -04:00
Sebastian Huber fa88e93d3d Add some POSIX header files for RTEMS
Add the POSIX header files

  * arpa/inet.h
  * net/if.h
  * netdb.h
  * netinet/in.h
  * netinet/tcp.h
  * sys/socket.h
  * sys/syslog.h
  * sys/uio.h
  * sys/un.h
  * syslog.h
  * termios.h

and their dependencies for RTEMS.  The origin of these files is the
latest FreeBSD.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-05-25 12:41:33 -04:00
Sebastian Huber 0b915d6be0 FreeBSD compatibility for RTEMS <sys/cpuset.h>
Make the RTEMS <sys/cpuset.h> compatible with the latest FreeBSD
version.

Fix the CPU_COPY() parameter order, see also:

https://devel.rtems.org/ticket/3023

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-05-25 12:36:27 -04:00
Sebastian Huber 0008601042 Increase MSIZE for RTEMS
Increase the MSIZE for RTEMS to be in line with the latest FreeBSD
version.  The legacy network stack of RTEMS will provides its own
definition.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-05-25 12:33:59 -04:00
Sebastian Huber 2efb117047 FreeBSD compatibility for RTEMS <sys/param.h>
Update the RTEMS <machine/param.h> and <sys/param.h> to be compatible
with the latest FreeBSD version.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-05-25 12:33:40 -04:00
Sebastian Huber e692cfc121 RTEMS: Harmonize MAXNAMLEN and NAME_MAX
Override MAXNAMLEN definition in <dirent.h> and make sure it equals
NAME_MAX.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-01-25 12:36:37 +01:00
Sebastian Huber 69dabb3e30 RTEMS: Add user-defined name to thread queues
Add a user-defined name to the self-contained synchronization objects in
order to make system diagnostics, tracing and debugging more user
friendly.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2017-01-09 16:11:50 +01:00
Sebastian Huber 724e21493a Self-contained pthread_spinlock_t for RTEMS
Turn pthread_spinlock_t into a self-contained object.  On uni-processor
configurations, interrupts are disabled in the lock/trylock operations
and the previous interrupt status is restored in the corresponding
unlock operations.  On SMP configurations, a ticket lock is a acquired
and released in addition.

See also:

https://devel.rtems.org/ticket/2674

This implementation is simple and efficient.  However, this test case of
the Linux Test Project would fail due to call of printf() and sleep()
during spin lock ownership:

https://github.com/linux-test-project/ltp/blob/master/testcases/open_posix_testsuite/conformance/interfaces/pthread_spin_lock/1-2.c

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2016-11-24 16:57:51 +01:00
Sebastian Huber e655d3d34b Move pthread types to <sys/_pthreadtypes.h>
This makes it possible provide operating system specific types for
<pthread.h>.  It is in line with the FreeBSD header file structure and
allows a future cleanup of <pthread.h> to not expose unrelated things
via <sys/types.h> and <unistd.h>.  Glibc uses the similar
<bits/pthreadtypes.h> for this purpose.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2016-11-17 11:33:15 +01:00
Sebastian Huber ba49de472f Add _TICKET_LOCK_INITIALIZER to <sys/lock.h>
Add _TICKET_LOCK_INITIALIZER to statically initialize a
_Ticket_lock_Control structure.  This makes it possible to embed a
ticket lock in other structures outside of <sys/lock.h>.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2016-11-17 11:28:45 +01:00
Sebastian Huber 9bbc5a34f2 Use __inline in <sys/lock.h> for RTEMS
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2016-11-17 11:28:21 +01:00
Sebastian Huber 4b3a664a20 Add kernel space header for <sys/lock.h> for RTEMS
Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2016-11-07 14:03:54 +01:00
Gedare Bloom 644e40dd9c rtems: add sys/mman.h
This file was copied verbatim from FreeBSD and is in sync
	with the FreeBSD svn version used by rtems-libbsd.

Signed-off-by: Joel Sherrill <joelemail@rtems.org>
2016-08-08 11:11:48 -05:00
Jeff Johnston de2a5d6cd3 Add _Thread_queue_Queue::_owner for RTEMS
Add _Thread_queue_Queue::_owner which will be used for the upcomming
priority inheritance implementation and an O(m) independence-preserving
protocol (OMIP) implementation.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2016-05-03 14:21:17 -04:00
Jeff Johnston 4f81837d78 Move _Thread_queue_Queue::_Lock for RTEMS
Move _Thread_queue_Queue::_Lock to begin of the structure.  On RTEMS,
the presence of a lock component in the thread queue structures actually
depends on the build-time RTEMS_SMP configuration option.  A move of
this part to the begin of the structure allows an implementation re-use
for the other parts.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2016-05-03 14:18:54 -04:00
Sebastian Huber 9672033c63 Drop <unistd.h> include from RTEMS <sys/param.h>
This include is not present in default Newlib, glibc and FreeBSD
<sys/param.h>.  With it there is now a conflict with <sys/libkern.h>
introduced by ecf453f963.

Signed-off-by: Sebastian Huber <sebastian.huber@embedded-brains.de>
2016-04-07 17:13:47 +02:00
Nick Withers 5644f71428 Add definitions for NBBY to arm and rtems targets
* libc/sys/arm/sys/param.h (NBBY): Define if not already defined.
	* libc/sys/rtems/include/sys/param.h (NBBY): Define.

Signed-off-by: Corinna Vinschen <corinna@vinschen.de>
2015-12-07 16:31:00 +01:00
Sebastian Huber b7ca25162a Fix typo in <sys/lock.h>
Sorry, there was a typo in <sys/lock.h> which leads to memory corruption
since not enough space is reserved for the lock object.

newlib/ChangeLog
2015-07-30  Sebastian Huber  <sebastian.huber@embedded-brains.de>

	* libc/sys/rtems/include/sys/lock.h (__LOCK_INIT_RECURSIVE): Use
	proper type.
2015-07-31 06:21:04 -07:00
Sebastian Huber f04e46b141 Move header files
During libgcc build the first include search path for <...> is
"../newlib/libc/sys/rtems/include".  Move all RTEMS specific header
files to "libc/sys/rtems/include" so that they can be found.  Later
during libc build the header files in the previous location were somehow
present, but for libgcc build they were invisible.  This change is
necessary to use <pthread.h> for the GCC thread model implementation.

newlib/ChangeLog
2015-07-27  Sebastian Huber  <sebastian.huber@embedded-brains.de>

	libc/sys/rtems/machine/_types.h: Move to ...
	libc/sys/rtems/include/machine/_types.h: ... here.
	libc/sys/rtems/machine/limits.h: Move to ...
	libc/sys/rtems/include/machine/limits.h: ... here.
	libc/sys/rtems/machine/param.h: Move to ...
	libc/sys/rtems/include/machine/param.h: ... here.
	libc/sys/rtems/sys/cpuset.h: Move to ...
	libc/sys/rtems/include/sys/cpuset.h: ... here.
	libc/sys/rtems/sys/dirent.h: Move to ...
	libc/sys/rtems/include/sys/dirent.h: ... here.
	libc/sys/rtems/sys/param.h: Move to ...
	libc/sys/rtems/include/sys/param.h: ... here.
	libc/sys/rtems/sys/syslimits.h: Move to ...
	libc/sys/rtems/include/sys/syslimits.h: ... here.
	libc/sys/rtems/sys/utime.h: Move to ...
	libc/sys/rtems/include/sys/utime.h: ... here.
2015-07-29 12:53:53 -07:00
Sebastian Huber ecaef05f66 Add <sys/lock.h>
Provide self-contained synchronization objects for RTEMS.  The API was
designed to be able to use it for the Newlib internal locks, the C11
threads support, the GCC thread model support and the libgomp operating
system configuration in combination with <pthread.h>.

References:

https://lists.rtems.org/pipermail/devel/2015-July/011989.html
https://lists.rtems.org/pipermail/devel/2015-July/012000.html

Tickets:

https://devel.rtems.org/ticket/1247
https://devel.rtems.org/ticket/2274.

v2: Fix typo in _Condition_Wait_recursive_timed() declaration.

newlib/ChangeLog
2015-07-28  Sebastian Huber  <sebastian.huber@embedded-brains.de>

	* libc/sys/rtems/include/sys/lock.h: New.
2015-07-29 11:30:05 -07:00