History log of /openbsd-current/sys/arch/hppa/hppa/trap.c
Revision (<<< Hide revision tags) (Show revision tags >>>) Date Author Comments
# 1.166 14-Apr-2024 jsg

with empty body loops, put final semicolon on a new line for readability
ok bluhm@ jca@


Revision tags: OPENBSD_7_5_BASE
# 1.165 05-Jan-2024 miod

Remove more __syscall() leftovers.


# 1.164 13-Dec-2023 miod

Fix syscall number bounds check computations.


# 1.163 13-Dec-2023 miod

Put back variable initialization mistakenly lost in 1.162


# 1.162 12-Dec-2023 deraadt

remove support for syscall(2) -- the "indirection system call" because
it is a dangerous alternative entry point for all system calls, and thus
incompatible with the precision system call entry point scheme we are
heading towards. This has been a 3-year mission:
First perl needed a code-generated wrapper to fake syscall(2) as a giant
switch table, then all the ports were cleaned with relatively minor fixes,
except for "go". "go" required two fixes -- 1) a framework issue with
old library versions, and 2) like perl, a fake syscall(2) wrapper to
handle ioctl(2) and sysctl(2) because "syscall(SYS_ioctl" occurs all over
the place in the "go" ecosystem because the "go developers" are plan9-loving
unix-hating folk who tried to build an ecosystem without allowing "ioctl".
ok kettenis, jsing, afresh1, sthen


Revision tags: OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.161 11-Feb-2023 deraadt

__syscall() is no longer neccessary since the system calls which needed
it are now unpadded
ok kettenis guenther


# 1.160 16-Jan-2023 deraadt

we spent far too long debugging a weird go library problem (incorrect
arguments to mmap) because it was using syscall(2) and that callpath
is invisible in ktrace. make it visible, it will now show "(via syscall)"
and such.
ok guenther


# 1.159 02-Nov-2022 guenther

Clean up more ancient history: since 2015 the libc stubs for
fork/vfork/__tfork haven't cared about the second return register.
So, stop setting retval[1] in kern_fork.c and stop setting the
second return register in the MD child_return() routines.

With the above, we have no multi-register return values on LP64,
so stop touching that register in the trapframe on those archs.

testing miod@ and aoyama@
ok miod@


Revision tags: OPENBSD_7_2_BASE
# 1.158 12-Aug-2022 miod

Make sure we don't pass uninitialized siginfo values to trapsignal(); from
clang via jsg@, ok jsg@


# 1.157 20-Jul-2022 deraadt

the _pad_ system calls from 2021/12/23 can go away
ok guenther


Revision tags: OPENBSD_7_1_BASE
# 1.156 23-Dec-2021 guenther

Roll the syscalls that have an off_t argument to remove the explicit padding.
Switch libc and ld.so to the generic stubs for these calls.
WARNING: reboot to updated kernel before installing libc or ld.so!

Time for a story...

When gcc (back in 1.x days) first implemented long long, it didn't (always)
pass 64bit arguments in 'aligned' registers/stack slots, with the result that
argument offsets didn't match structure offsets. This affected the nine system
calls that pass off_t arguments:
ftruncate lseek mmap mquery pread preadv pwrite pwritev truncate

To avoid having to do custom ASM wrappers for those, BSD put an explicit pad
argument in so that the off_t argument would always start on a even slot and
thus be naturally aligned. Thus those odd wrappers in lib/libc/sys/ that use
__syscall() and pass an extra '0' argument.

The ABIs for different CPUs eventually settled how things should be passed on
each and gcc 2.x followed them. The only arch now where it helps is landisk,
which needs to skip the last argument register if it would be the first half of
a 64bit argument. So: add new syscalls without the pad argument and on landisk
do that skipping directly in the syscall handler in the kernel. Keep compat
support for the existing syscalls long enough for the transition.

ok deraadt@


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.165 05-Jan-2024 miod

Remove more __syscall() leftovers.


# 1.164 13-Dec-2023 miod

Fix syscall number bounds check computations.


# 1.163 13-Dec-2023 miod

Put back variable initialization mistakenly lost in 1.162


# 1.162 12-Dec-2023 deraadt

remove support for syscall(2) -- the "indirection system call" because
it is a dangerous alternative entry point for all system calls, and thus
incompatible with the precision system call entry point scheme we are
heading towards. This has been a 3-year mission:
First perl needed a code-generated wrapper to fake syscall(2) as a giant
switch table, then all the ports were cleaned with relatively minor fixes,
except for "go". "go" required two fixes -- 1) a framework issue with
old library versions, and 2) like perl, a fake syscall(2) wrapper to
handle ioctl(2) and sysctl(2) because "syscall(SYS_ioctl" occurs all over
the place in the "go" ecosystem because the "go developers" are plan9-loving
unix-hating folk who tried to build an ecosystem without allowing "ioctl".
ok kettenis, jsing, afresh1, sthen


Revision tags: OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.161 11-Feb-2023 deraadt

__syscall() is no longer neccessary since the system calls which needed
it are now unpadded
ok kettenis guenther


# 1.160 16-Jan-2023 deraadt

we spent far too long debugging a weird go library problem (incorrect
arguments to mmap) because it was using syscall(2) and that callpath
is invisible in ktrace. make it visible, it will now show "(via syscall)"
and such.
ok guenther


# 1.159 02-Nov-2022 guenther

Clean up more ancient history: since 2015 the libc stubs for
fork/vfork/__tfork haven't cared about the second return register.
So, stop setting retval[1] in kern_fork.c and stop setting the
second return register in the MD child_return() routines.

With the above, we have no multi-register return values on LP64,
so stop touching that register in the trapframe on those archs.

testing miod@ and aoyama@
ok miod@


Revision tags: OPENBSD_7_2_BASE
# 1.158 12-Aug-2022 miod

Make sure we don't pass uninitialized siginfo values to trapsignal(); from
clang via jsg@, ok jsg@


# 1.157 20-Jul-2022 deraadt

the _pad_ system calls from 2021/12/23 can go away
ok guenther


Revision tags: OPENBSD_7_1_BASE
# 1.156 23-Dec-2021 guenther

Roll the syscalls that have an off_t argument to remove the explicit padding.
Switch libc and ld.so to the generic stubs for these calls.
WARNING: reboot to updated kernel before installing libc or ld.so!

Time for a story...

When gcc (back in 1.x days) first implemented long long, it didn't (always)
pass 64bit arguments in 'aligned' registers/stack slots, with the result that
argument offsets didn't match structure offsets. This affected the nine system
calls that pass off_t arguments:
ftruncate lseek mmap mquery pread preadv pwrite pwritev truncate

To avoid having to do custom ASM wrappers for those, BSD put an explicit pad
argument in so that the off_t argument would always start on a even slot and
thus be naturally aligned. Thus those odd wrappers in lib/libc/sys/ that use
__syscall() and pass an extra '0' argument.

The ABIs for different CPUs eventually settled how things should be passed on
each and gcc 2.x followed them. The only arch now where it helps is landisk,
which needs to skip the last argument register if it would be the first half of
a 64bit argument. So: add new syscalls without the pad argument and on landisk
do that skipping directly in the syscall handler in the kernel. Keep compat
support for the existing syscalls long enough for the transition.

ok deraadt@


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.164 13-Dec-2023 miod

Fix syscall number bounds check computations.


# 1.163 13-Dec-2023 miod

Put back variable initialization mistakenly lost in 1.162


# 1.162 12-Dec-2023 deraadt

remove support for syscall(2) -- the "indirection system call" because
it is a dangerous alternative entry point for all system calls, and thus
incompatible with the precision system call entry point scheme we are
heading towards. This has been a 3-year mission:
First perl needed a code-generated wrapper to fake syscall(2) as a giant
switch table, then all the ports were cleaned with relatively minor fixes,
except for "go". "go" required two fixes -- 1) a framework issue with
old library versions, and 2) like perl, a fake syscall(2) wrapper to
handle ioctl(2) and sysctl(2) because "syscall(SYS_ioctl" occurs all over
the place in the "go" ecosystem because the "go developers" are plan9-loving
unix-hating folk who tried to build an ecosystem without allowing "ioctl".
ok kettenis, jsing, afresh1, sthen


Revision tags: OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.161 11-Feb-2023 deraadt

__syscall() is no longer neccessary since the system calls which needed
it are now unpadded
ok kettenis guenther


# 1.160 16-Jan-2023 deraadt

we spent far too long debugging a weird go library problem (incorrect
arguments to mmap) because it was using syscall(2) and that callpath
is invisible in ktrace. make it visible, it will now show "(via syscall)"
and such.
ok guenther


# 1.159 02-Nov-2022 guenther

Clean up more ancient history: since 2015 the libc stubs for
fork/vfork/__tfork haven't cared about the second return register.
So, stop setting retval[1] in kern_fork.c and stop setting the
second return register in the MD child_return() routines.

With the above, we have no multi-register return values on LP64,
so stop touching that register in the trapframe on those archs.

testing miod@ and aoyama@
ok miod@


Revision tags: OPENBSD_7_2_BASE
# 1.158 12-Aug-2022 miod

Make sure we don't pass uninitialized siginfo values to trapsignal(); from
clang via jsg@, ok jsg@


# 1.157 20-Jul-2022 deraadt

the _pad_ system calls from 2021/12/23 can go away
ok guenther


Revision tags: OPENBSD_7_1_BASE
# 1.156 23-Dec-2021 guenther

Roll the syscalls that have an off_t argument to remove the explicit padding.
Switch libc and ld.so to the generic stubs for these calls.
WARNING: reboot to updated kernel before installing libc or ld.so!

Time for a story...

When gcc (back in 1.x days) first implemented long long, it didn't (always)
pass 64bit arguments in 'aligned' registers/stack slots, with the result that
argument offsets didn't match structure offsets. This affected the nine system
calls that pass off_t arguments:
ftruncate lseek mmap mquery pread preadv pwrite pwritev truncate

To avoid having to do custom ASM wrappers for those, BSD put an explicit pad
argument in so that the off_t argument would always start on a even slot and
thus be naturally aligned. Thus those odd wrappers in lib/libc/sys/ that use
__syscall() and pass an extra '0' argument.

The ABIs for different CPUs eventually settled how things should be passed on
each and gcc 2.x followed them. The only arch now where it helps is landisk,
which needs to skip the last argument register if it would be the first half of
a 64bit argument. So: add new syscalls without the pad argument and on landisk
do that skipping directly in the syscall handler in the kernel. Keep compat
support for the existing syscalls long enough for the transition.

ok deraadt@


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.164 13-Dec-2023 miod

Fix syscall number bounds check computations.


# 1.163 13-Dec-2023 miod

Put back variable initialization mistakenly lost in 1.162


# 1.162 12-Dec-2023 deraadt

remove support for syscall(2) -- the "indirection system call" because
it is a dangerous alternative entry point for all system calls, and thus
incompatible with the precision system call entry point scheme we are
heading towards. This has been a 3-year mission:
First perl needed a code-generated wrapper to fake syscall(2) as a giant
switch table, then all the ports were cleaned with relatively minor fixes,
except for "go". "go" required two fixes -- 1) a framework issue with
old library versions, and 2) like perl, a fake syscall(2) wrapper to
handle ioctl(2) and sysctl(2) because "syscall(SYS_ioctl" occurs all over
the place in the "go" ecosystem because the "go developers" are plan9-loving
unix-hating folk who tried to build an ecosystem without allowing "ioctl".
ok kettenis, jsing, afresh1, sthen


Revision tags: OPENBSD_7_3_BASE OPENBSD_7_4_BASE
# 1.161 11-Feb-2023 deraadt

__syscall() is no longer neccessary since the system calls which needed
it are now unpadded
ok kettenis guenther


# 1.160 16-Jan-2023 deraadt

we spent far too long debugging a weird go library problem (incorrect
arguments to mmap) because it was using syscall(2) and that callpath
is invisible in ktrace. make it visible, it will now show "(via syscall)"
and such.
ok guenther


# 1.159 02-Nov-2022 guenther

Clean up more ancient history: since 2015 the libc stubs for
fork/vfork/__tfork haven't cared about the second return register.
So, stop setting retval[1] in kern_fork.c and stop setting the
second return register in the MD child_return() routines.

With the above, we have no multi-register return values on LP64,
so stop touching that register in the trapframe on those archs.

testing miod@ and aoyama@
ok miod@


Revision tags: OPENBSD_7_2_BASE
# 1.158 12-Aug-2022 miod

Make sure we don't pass uninitialized siginfo values to trapsignal(); from
clang via jsg@, ok jsg@


# 1.157 20-Jul-2022 deraadt

the _pad_ system calls from 2021/12/23 can go away
ok guenther


Revision tags: OPENBSD_7_1_BASE
# 1.156 23-Dec-2021 guenther

Roll the syscalls that have an off_t argument to remove the explicit padding.
Switch libc and ld.so to the generic stubs for these calls.
WARNING: reboot to updated kernel before installing libc or ld.so!

Time for a story...

When gcc (back in 1.x days) first implemented long long, it didn't (always)
pass 64bit arguments in 'aligned' registers/stack slots, with the result that
argument offsets didn't match structure offsets. This affected the nine system
calls that pass off_t arguments:
ftruncate lseek mmap mquery pread preadv pwrite pwritev truncate

To avoid having to do custom ASM wrappers for those, BSD put an explicit pad
argument in so that the off_t argument would always start on a even slot and
thus be naturally aligned. Thus those odd wrappers in lib/libc/sys/ that use
__syscall() and pass an extra '0' argument.

The ABIs for different CPUs eventually settled how things should be passed on
each and gcc 2.x followed them. The only arch now where it helps is landisk,
which needs to skip the last argument register if it would be the first half of
a 64bit argument. So: add new syscalls without the pad argument and on landisk
do that skipping directly in the syscall handler in the kernel. Keep compat
support for the existing syscalls long enough for the transition.

ok deraadt@


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.161 11-Feb-2023 deraadt

__syscall() is no longer neccessary since the system calls which needed
it are now unpadded
ok kettenis guenther


# 1.160 16-Jan-2023 deraadt

we spent far too long debugging a weird go library problem (incorrect
arguments to mmap) because it was using syscall(2) and that callpath
is invisible in ktrace. make it visible, it will now show "(via syscall)"
and such.
ok guenther


# 1.159 02-Nov-2022 guenther

Clean up more ancient history: since 2015 the libc stubs for
fork/vfork/__tfork haven't cared about the second return register.
So, stop setting retval[1] in kern_fork.c and stop setting the
second return register in the MD child_return() routines.

With the above, we have no multi-register return values on LP64,
so stop touching that register in the trapframe on those archs.

testing miod@ and aoyama@
ok miod@


Revision tags: OPENBSD_7_2_BASE
# 1.158 12-Aug-2022 miod

Make sure we don't pass uninitialized siginfo values to trapsignal(); from
clang via jsg@, ok jsg@


# 1.157 20-Jul-2022 deraadt

the _pad_ system calls from 2021/12/23 can go away
ok guenther


Revision tags: OPENBSD_7_1_BASE
# 1.156 23-Dec-2021 guenther

Roll the syscalls that have an off_t argument to remove the explicit padding.
Switch libc and ld.so to the generic stubs for these calls.
WARNING: reboot to updated kernel before installing libc or ld.so!

Time for a story...

When gcc (back in 1.x days) first implemented long long, it didn't (always)
pass 64bit arguments in 'aligned' registers/stack slots, with the result that
argument offsets didn't match structure offsets. This affected the nine system
calls that pass off_t arguments:
ftruncate lseek mmap mquery pread preadv pwrite pwritev truncate

To avoid having to do custom ASM wrappers for those, BSD put an explicit pad
argument in so that the off_t argument would always start on a even slot and
thus be naturally aligned. Thus those odd wrappers in lib/libc/sys/ that use
__syscall() and pass an extra '0' argument.

The ABIs for different CPUs eventually settled how things should be passed on
each and gcc 2.x followed them. The only arch now where it helps is landisk,
which needs to skip the last argument register if it would be the first half of
a 64bit argument. So: add new syscalls without the pad argument and on landisk
do that skipping directly in the syscall handler in the kernel. Keep compat
support for the existing syscalls long enough for the transition.

ok deraadt@


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.160 16-Jan-2023 deraadt

we spent far too long debugging a weird go library problem (incorrect
arguments to mmap) because it was using syscall(2) and that callpath
is invisible in ktrace. make it visible, it will now show "(via syscall)"
and such.
ok guenther


# 1.159 02-Nov-2022 guenther

Clean up more ancient history: since 2015 the libc stubs for
fork/vfork/__tfork haven't cared about the second return register.
So, stop setting retval[1] in kern_fork.c and stop setting the
second return register in the MD child_return() routines.

With the above, we have no multi-register return values on LP64,
so stop touching that register in the trapframe on those archs.

testing miod@ and aoyama@
ok miod@


Revision tags: OPENBSD_7_2_BASE
# 1.158 12-Aug-2022 miod

Make sure we don't pass uninitialized siginfo values to trapsignal(); from
clang via jsg@, ok jsg@


# 1.157 20-Jul-2022 deraadt

the _pad_ system calls from 2021/12/23 can go away
ok guenther


Revision tags: OPENBSD_7_1_BASE
# 1.156 23-Dec-2021 guenther

Roll the syscalls that have an off_t argument to remove the explicit padding.
Switch libc and ld.so to the generic stubs for these calls.
WARNING: reboot to updated kernel before installing libc or ld.so!

Time for a story...

When gcc (back in 1.x days) first implemented long long, it didn't (always)
pass 64bit arguments in 'aligned' registers/stack slots, with the result that
argument offsets didn't match structure offsets. This affected the nine system
calls that pass off_t arguments:
ftruncate lseek mmap mquery pread preadv pwrite pwritev truncate

To avoid having to do custom ASM wrappers for those, BSD put an explicit pad
argument in so that the off_t argument would always start on a even slot and
thus be naturally aligned. Thus those odd wrappers in lib/libc/sys/ that use
__syscall() and pass an extra '0' argument.

The ABIs for different CPUs eventually settled how things should be passed on
each and gcc 2.x followed them. The only arch now where it helps is landisk,
which needs to skip the last argument register if it would be the first half of
a 64bit argument. So: add new syscalls without the pad argument and on landisk
do that skipping directly in the syscall handler in the kernel. Keep compat
support for the existing syscalls long enough for the transition.

ok deraadt@


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.159 02-Nov-2022 guenther

Clean up more ancient history: since 2015 the libc stubs for
fork/vfork/__tfork haven't cared about the second return register.
So, stop setting retval[1] in kern_fork.c and stop setting the
second return register in the MD child_return() routines.

With the above, we have no multi-register return values on LP64,
so stop touching that register in the trapframe on those archs.

testing miod@ and aoyama@
ok miod@


Revision tags: OPENBSD_7_2_BASE
# 1.158 12-Aug-2022 miod

Make sure we don't pass uninitialized siginfo values to trapsignal(); from
clang via jsg@, ok jsg@


# 1.157 20-Jul-2022 deraadt

the _pad_ system calls from 2021/12/23 can go away
ok guenther


Revision tags: OPENBSD_7_1_BASE
# 1.156 23-Dec-2021 guenther

Roll the syscalls that have an off_t argument to remove the explicit padding.
Switch libc and ld.so to the generic stubs for these calls.
WARNING: reboot to updated kernel before installing libc or ld.so!

Time for a story...

When gcc (back in 1.x days) first implemented long long, it didn't (always)
pass 64bit arguments in 'aligned' registers/stack slots, with the result that
argument offsets didn't match structure offsets. This affected the nine system
calls that pass off_t arguments:
ftruncate lseek mmap mquery pread preadv pwrite pwritev truncate

To avoid having to do custom ASM wrappers for those, BSD put an explicit pad
argument in so that the off_t argument would always start on a even slot and
thus be naturally aligned. Thus those odd wrappers in lib/libc/sys/ that use
__syscall() and pass an extra '0' argument.

The ABIs for different CPUs eventually settled how things should be passed on
each and gcc 2.x followed them. The only arch now where it helps is landisk,
which needs to skip the last argument register if it would be the first half of
a 64bit argument. So: add new syscalls without the pad argument and on landisk
do that skipping directly in the syscall handler in the kernel. Keep compat
support for the existing syscalls long enough for the transition.

ok deraadt@


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.158 12-Aug-2022 miod

Make sure we don't pass uninitialized siginfo values to trapsignal(); from
clang via jsg@, ok jsg@


# 1.157 20-Jul-2022 deraadt

the _pad_ system calls from 2021/12/23 can go away
ok guenther


Revision tags: OPENBSD_7_1_BASE
# 1.156 23-Dec-2021 guenther

Roll the syscalls that have an off_t argument to remove the explicit padding.
Switch libc and ld.so to the generic stubs for these calls.
WARNING: reboot to updated kernel before installing libc or ld.so!

Time for a story...

When gcc (back in 1.x days) first implemented long long, it didn't (always)
pass 64bit arguments in 'aligned' registers/stack slots, with the result that
argument offsets didn't match structure offsets. This affected the nine system
calls that pass off_t arguments:
ftruncate lseek mmap mquery pread preadv pwrite pwritev truncate

To avoid having to do custom ASM wrappers for those, BSD put an explicit pad
argument in so that the off_t argument would always start on a even slot and
thus be naturally aligned. Thus those odd wrappers in lib/libc/sys/ that use
__syscall() and pass an extra '0' argument.

The ABIs for different CPUs eventually settled how things should be passed on
each and gcc 2.x followed them. The only arch now where it helps is landisk,
which needs to skip the last argument register if it would be the first half of
a 64bit argument. So: add new syscalls without the pad argument and on landisk
do that skipping directly in the syscall handler in the kernel. Keep compat
support for the existing syscalls long enough for the transition.

ok deraadt@


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.157 20-Jul-2022 deraadt

the _pad_ system calls from 2021/12/23 can go away
ok guenther


Revision tags: OPENBSD_7_1_BASE
# 1.156 23-Dec-2021 guenther

Roll the syscalls that have an off_t argument to remove the explicit padding.
Switch libc and ld.so to the generic stubs for these calls.
WARNING: reboot to updated kernel before installing libc or ld.so!

Time for a story...

When gcc (back in 1.x days) first implemented long long, it didn't (always)
pass 64bit arguments in 'aligned' registers/stack slots, with the result that
argument offsets didn't match structure offsets. This affected the nine system
calls that pass off_t arguments:
ftruncate lseek mmap mquery pread preadv pwrite pwritev truncate

To avoid having to do custom ASM wrappers for those, BSD put an explicit pad
argument in so that the off_t argument would always start on a even slot and
thus be naturally aligned. Thus those odd wrappers in lib/libc/sys/ that use
__syscall() and pass an extra '0' argument.

The ABIs for different CPUs eventually settled how things should be passed on
each and gcc 2.x followed them. The only arch now where it helps is landisk,
which needs to skip the last argument register if it would be the first half of
a 64bit argument. So: add new syscalls without the pad argument and on landisk
do that skipping directly in the syscall handler in the kernel. Keep compat
support for the existing syscalls long enough for the transition.

ok deraadt@


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.156 23-Dec-2021 guenther

Roll the syscalls that have an off_t argument to remove the explicit padding.
Switch libc and ld.so to the generic stubs for these calls.
WARNING: reboot to updated kernel before installing libc or ld.so!

Time for a story...

When gcc (back in 1.x days) first implemented long long, it didn't (always)
pass 64bit arguments in 'aligned' registers/stack slots, with the result that
argument offsets didn't match structure offsets. This affected the nine system
calls that pass off_t arguments:
ftruncate lseek mmap mquery pread preadv pwrite pwritev truncate

To avoid having to do custom ASM wrappers for those, BSD put an explicit pad
argument in so that the off_t argument would always start on a even slot and
thus be naturally aligned. Thus those odd wrappers in lib/libc/sys/ that use
__syscall() and pass an extra '0' argument.

The ABIs for different CPUs eventually settled how things should be passed on
each and gcc 2.x followed them. The only arch now where it helps is landisk,
which needs to skip the last argument register if it would be the first half of
a 64bit argument. So: add new syscalls without the pad argument and on landisk
do that skipping directly in the syscall handler in the kernel. Keep compat
support for the existing syscalls long enough for the transition.

ok deraadt@


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.155 09-Dec-2021 guenther

We only have one syscall table: inline sysent/SYS_MAXSYSCALL and
SYS_syscall as the nosys() function into the MD syscall entry
routines and the SYSCALL_DEBUG support. Adjust alpha's syscall
check to match the other archs. Also, make sysent const to get it
into .rodata.

With that, 'struct emul' is unused: delete it and all its references

ok millert@


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.154 07-Oct-2021 claudio

trapsignal() can be called without KERNEL_LOCK, adjust code accordingly.
OK mpi@


Revision tags: OPENBSD_6_9_BASE OPENBSD_7_0_BASE
# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.153 11-Mar-2021 jsg

spelling


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.152 22-Oct-2020 deraadt

uvm_grow() doesn't need KERNEL_LOCK anymore, and onfault never did.
minor refactorings to narrow KERNEL_LOCK just around uvm_fault()
ok kettenis


# 1.151 21-Oct-2020 deraadt

move the backwards-stack vm_minsaddr check from hppa trap.c to uvm_grow(),
within the correct #ifdef of course.
ok kettenis


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.150 08-Oct-2020 deraadt

use access_type as the PROT_* variable for uvm_fault() consistantly
ok kettenis


Revision tags: OPENBSD_6_8_BASE
# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.149 24-Sep-2020 deraadt

Only perform uvm_map_inentry() checks for PROC_SP for userland pagefaults.
This should be sufficient for identifying pivoted ROP. Doing so for other
traps is at best opportunistic for finding a straight-running ROP chain,
but the added (and rare) sleeping point has proven to be dangerous.
Discussed at length with kettenis and mortimer.
ok mortimer kettenis mpi


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.148 14-Sep-2020 kettenis

When emulating reading from cr26 and cr27 on PCXS, simply set cr26 to zero
and take the cr27 value that was saved in the trap frame. This matches
what process_read_regs() does and prevents returning the wrong values if
the uvm_map_inentry() checks sleeps trying to grab the vm_map read lock.

ok miod@, deraadt@


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.147 19-Aug-2020 mpi

Push KERNEL_LOCK/UNLOCK() dance inside trapsignal().

ok kettenis@, visa@


Revision tags: OPENBSD_6_6_BASE OPENBSD_6_7_BASE
# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.146 06-Sep-2019 deraadt

If uvm_map_inentry returns false then a signal has been delivered, and
userret() must be called on trap() exit to deliver it, rather than
repeating the same cause infinitely. discovered by George Koehler
ok kettenis bluhm visa


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.145 09-Jul-2019 deraadt

I wrote the pc-page-writeable and sp-not-MAP_STACK code to be shared, and
then ran into the messaging being poor. Then I fixed the messages. But
there are two sub-cases of sp-not-MAP_STACK -- one at syscall time, and
another at regular userland trap (on some architectures), and I bungled
that messaging. Correct that now, while I look for yet another better way...
discovered by millert, who ran an pre-MAP_STACK binary.


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.144 01-Jun-2019 deraadt

Refactor the MAP_STACK feature, and introduce another similar variation:
Lookup the address that a syscall instruction is executed from, and kill
the process if that page is writeable. This brings an aspect of W^X
behaviour to W|X mappings (in JITs not yet adapted to W^X). The goal is
to remove simple attack methods and force use of ret2libc or other more
complicated means.
ok kettenis stefan visa


Revision tags: OPENBSD_6_4_BASE OPENBSD_6_5_BASE
# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


# 1.143 12-Apr-2018 deraadt

Implement MAP_STACK option for mmap(). Synchronous faults (pagefault and
syscall) confirm the stack register points at MAP_STACK memory, otherwise
SIGSEGV is delivered. sigaltstack() and pthread_attr_setstack() are modified
to create a MAP_STACK sub-region which satisfies alignment requirements.
Observe that MAP_STACK can only be set/cleared by mmap(), which zeroes the
contents of the region -- there is no mprotect() equivalent operation, so
there is no MAP_STACK-adding gadget.
This opportunistic software-emulation of a stack protection bit makes
stack-pivot operations during ROPchain fragile (kind of like removing a
tool from the toolbox).
original discussion with tedu, uvm work by stefan, testing by mortimer
ok kettenis


Revision tags: OPENBSD_6_2_BASE OPENBSD_6_3_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe


Revision tags: OPENBSD_6_2_BASE
# 1.142 22-Jul-2017 kettenis

Fix the various SIGSEGV and SIGBUS scenarios to match what we do on
amd64/arm64/armv7/i386/sparc64.

ok visa@


Revision tags: OPENBSD_6_1_BASE
# 1.141 19-Oct-2016 guenther

Change process_{domem,auxv_offset}() to take a process instead of a proc.
Make process_auxv_offset() take and release a reference of the vmspace like
process_domem() does.

ok kettenis@


# 1.140 08-Oct-2016 guenther

Various printf claim to report the PID, so actually report that and not the TID

Build testing assistance from deraadt@


Revision tags: OPENBSD_6_0_BASE
# 1.139 27-Feb-2016 mpi

Rename kdb_trap() into db_ktrap().

The goal is to include it in the list of functions that must not be
instrumented. All ddb(8) functions should be in this list and have
their names start with 'db_'.

ok visa@, deraadt@


Revision tags: OPENBSD_5_7_BASE OPENBSD_5_8_BASE OPENBSD_5_9_BASE
# 1.138 16-Nov-2014 deraadt

Replace a plethora of historical protection options with just
PROT_NONE, PROT_READ, PROT_WRITE, and PROT_EXEC from mman.h.
PROT_MASK is introduced as the one true way of extracting those bits.
Remove UVM_ADV_* wrapper, using the standard names.
ok doug guenther kettenis


# 1.137 08-Oct-2014 deraadt

#ifndef DDB, correct a format string..


Revision tags: OPENBSD_5_6_BASE
# 1.136 07-Jul-2014 miod

Do the 64-bit argument swap dance, for the syscalls which require it,
unconditionaly instead of only doing it if invoked indirectly through syscall
or __syscall.

While there, tweak stack argument fetching to only perform one large copyin()
call, rather than a loop of small ones.

ok kettenis@


# 1.135 13-Jun-2014 tobiasu

Fix ptrace() hanging hppa MP systems

Fiddling with uvm, registers and breakpoints requires taking the kernel lock
Problem spotted by tedu@

ok kettenis@ deraadt@ miod@


# 1.134 11-May-2014 guenther

Move the increment of uvmexp.softs back to the caller of mi_ast():
it needs to be done atomicly on some MP archs and we don't have
atomic_add_int() everywhere yet. Also, mi_ast() was meant to be inline.

noted by miod@


# 1.133 10-May-2014 deraadt

sigh, another case of user.h before syscall_mi.h


# 1.132 10-May-2014 guenther

Factor out the common ast bits into mi_ast()

ok deraadt@


# 1.131 08-May-2014 miod

Format string fixes and removal of -Wno-format for hppa kernels.


# 1.130 18-Apr-2014 guenther

Have each thread keeps its own (counted!) reference to the process's ucreds
to avoid possible use-after-free references when swapping ids in threaded
processes. "Do I have the right creds?" checks are always made with the
threads creds.

Inspired by FreeBSD and NetBSD
"right time" deraadt@


# 1.129 08-Apr-2014 mpi

Less <uvm/uvm.h>


# 1.128 26-Mar-2014 guenther

Move p_emul and p_sigcode from proc to process.
Tweak the handling of ktrace EMUL when changing ktracing: only
generate one per process (not one per thread) and pass the correct
proc pointer down to the VFS layer. Permit generating of NAMI and
CSW records inside ktrace(2) itself.

ok deraadt@ millert@


Revision tags: OPENBSD_5_4_BASE OPENBSD_5_5_BASE
# 1.127 10-Apr-2013 miod

Correctly handle data memory protection ID traps: if occuring in user mode,
SIGSEGV the process; if occuring in kernel mode, check for a possible
pcb_onfault to recover, before otherwise panicing.

ok kettenis@


Revision tags: OPENBSD_5_3_BASE
# 1.126 31-Dec-2012 guenther

Remove emulation errno mapping code from platforms that no longer have
non-native emulations.

ok miod@


# 1.125 29-Aug-2012 kettenis

Handle T_IPROT traps from userland by call vm_fault() instead of doing an
unconditional SIGSEGV. Fixes random SIGSEGVs during single-stepping.

ok miod@


# 1.124 07-Aug-2012 kettenis

Don't include <sys/ktrace.h>; makes this file compile again.


# 1.123 07-Aug-2012 guenther

Move the common bits of syscall invocation and return handling into
an MI file, <sys/syscall_mi.h>, correcting inconsistencies and the
handling when copyin() of arguments fails.

Tested on i386, amd64, sparc64, and alpha (thanks naddy@)
Any issues with other platforms will be fixed in tree.

header name from millert@; ok miod@


Revision tags: OPENBSD_5_2_BASE
# 1.122 11-Apr-2012 mikeb

The first ktrace record for a newly spawned thread is a return
from a fork syscall done by the parent. Use __tfork, not rfork
here to match the ktrace records for the parent (CALL __tfork,
RET __tfork). ok guenther


Revision tags: OPENBSD_5_1_BASE
# 1.121 16-Nov-2011 deraadt

oops, need a prototype for ast()


# 1.120 16-Nov-2011 deraadt

Make userret() MI. On architectures which jammed stuff into it in the
past, pull that code out seperately.
ok guenther miod


# 1.119 08-Nov-2011 miod

On PCXS processors, reading %cr26 and %cr27 from userland causes a
privileged register trap. Cope with this in the trap handler to let
userland use these registers; this will be necessary in the not-so-distant
future.


Revision tags: OPENBSD_5_0_BASE
# 1.118 11-Jul-2011 guenther

Revert art@'s moving around of the KERNEL_LOCK()/KERNEL_UNLOCK() calls,
as it causes hangs in some ports, including libsigsegv's configure script

confirmed by krw@, landry@


# 1.117 07-Jul-2011 art

There is a bunch of places in the kernel entry points where we don't
hold the kernel lock, but still need call one function that needs it.

Instead of grabbing the lock all over the place, move the locks into
the affected functions: trapsignal, scdebug*, ktrsyscall, ktrsysret,
systrace_redirect and ADDUPROF. In the cases we already hold the biglock
we'll just recurse.

kettenis@, beck@ ok


# 1.116 06-Jul-2011 art

Clean up after P_BIGLOCK removal.
KERNEL_PROC_LOCK -> KERNEL_LOCK
KERNEL_PROC_UNLOCK -> KERNEL_UNLOCK

oga@ ok


# 1.115 06-Jun-2011 jsing

Fix whitespace and use #ifdef TRAPDEBUG instead of #if 0.


# 1.114 03-Apr-2011 guenther

Move PPWAIT flag from struct proc to process, so that rthreads in
a vforked child behave correctly. Have the parent in a vfork()
wait on a (different) flag in *its* process instead of the child
to prevent a possible use-after-free. When ktracing the child
return from a fork, call it rfork if an rthread was created.

ok blambert@


Revision tags: OPENBSD_4_9_BASE
# 1.113 23-Jan-2011 jsing

Ansify function.


# 1.112 21-Dec-2010 claudio

Convert netisr to a normal soft interrupt instead of hanving MD code
for it. This makes the netisr a real C function which will help further
development. No noticable performance change on i386 and amd64.
With input from kettenis@ and miod@ additional OKs mikeb@ and henning@


# 1.111 27-Nov-2010 miod

Remove ddb single-step load and store counters. Most platforms do not
implement them, and they are of questionable usefulness.


Revision tags: OPENBSD_4_8_BASE
# 1.110 01-Jul-2010 jsing

Create a struct to store FP state and include a pointer to the CPU that
currently holds the FPU context for this process. This will be soon used
to implement FPU shootdowns on multiprocessor kernels.

ok kettenis@


# 1.109 26-Jun-2010 jsing

Do not store curcpu() in syscall and trap handlers since we can go to
sleep and wake up on another CPU.

ok kettenis@


# 1.108 24-May-2010 deraadt

Add missing prototypes
ok jsing kettenis


# 1.107 09-May-2010 kettenis

Make single stepping a system call work. Instead of single stepping through
the syscall gateway page, which doesn't work since that page is shared
between processes, this makes us step over that bit by setting a breakpoint
on the instruction where the system call returns.

ok miod@, jsing@


# 1.106 30-Mar-2010 kettenis

Since we use a direct mapping for the area where we save a process' floating
point registers, we no longer need to flush/purge the cache.


# 1.105 27-Mar-2010 jsing

Add locking to trap and syscall handlers.

ok kettenis@ miod@


Revision tags: OPENBSD_4_7_BASE
# 1.104 31-Dec-2009 jsing

Make cpl and cpu_inintr per CPU variables. For locore.S, cpl becomes an
offset within cpu_primary_info - this will need to be revisited shortly.

ok kettenis@


# 1.103 29-Dec-2009 jsing

Make want_resched a per CPU variable.

ok kettenis@ miod@


# 1.102 29-Dec-2009 jsing

Move hppa to per process AST.

ok kettenis@ miod@


Revision tags: OPENBSD_4_5_BASE OPENBSD_4_6_BASE
# 1.101 04-Feb-2009 miod

Handle PCXS data protection traps. Unlike other PA1.1 chips, there is a
single trap code for alignment and protection faults, so we have to
figure out which kind of problem we are facing.

ok kettenis@


Revision tags: OPENBSD_4_3_BASE OPENBSD_4_4_BASE
# 1.100 15-Sep-2007 krw

realy -> really in comments. Most reported on tech@ by Jung.


Revision tags: OPENBSD_4_2_BASE
# 1.99 14-May-2007 martin

move hppa to __HAVE_CPUINFO

input from miod@, ok kettenis@


# 1.98 15-Mar-2007 art

Since p_flag is often manipulated in interrupts and without biglock
it's a good idea to use atomic.h operations on it. This mechanic
change updates all bit operations on p_flag to atomic_{set,clear}bits_int.

Only exception is that P_OWEUPC is set by MI code before calling
need_proftick and it's automatically cleared by ADDUPC. There's
no reason for MD handling of that flag since everyone handles it the
same way.

kettenis@ ok


Revision tags: OPENBSD_4_1_BASE
# 1.97 05-Mar-2007 mickey

proper check for [non]user returns to syscall gate page; found by miod@


# 1.96 17-Jan-2007 mickey

new shorter version of userret() as surely miod ment it to be like (; miod@ ok


# 1.95 15-Jan-2007 mickey

undo last broken userret() change here; miod@ ok


# 1.94 24-Dec-2006 miod

Check for want_resched when processing AST and nowhere else. But then, when
doing so, do not check for signals - userret() will do this.


Revision tags: OPENBSD_3_9_BASE OPENBSD_4_0_BASE
# 1.93 12-Dec-2005 miod

Nuke leftover references to procfs_mem.h.


# 1.92 11-Dec-2005 miod

Replace procfs_domem() with a similar interface, process_domem(), which lives
out of procfs and gets a ptrace request PT_{READ,WRITE}_{I,D} as argument;
also procfs_checkioperm() becomes process_checkioperm().

From art@ some time ago; ok kettenis@ pedro@


# 1.91 26-Oct-2005 martin

no more hppa_round_page() and hppa_trunc_page() macros

ok mickey@


# 1.90 15-Sep-2005 miod

In syscall(), do not recompute the struct proc * after the syscall has
succeeded. This used to be necessary for fork(), when returning in the child,
but we return in the child in child_return() which does TRT.

ok art@


# 1.89 15-Sep-2005 miod

Change child_return() to record a proper ktrace record for vfork child
processes.

ok art@ uwe@ (some time ago)


Revision tags: OPENBSD_3_8_BASE
# 1.88 14-Aug-2005 miod

Add mquery to the system calls which need extra care due to off_t arguments
and reversed stack direction; ok mickey@


# 1.87 18-Jul-2005 fgsch

remove trailing newline in panic(9); ok millert@ and deraadt@


# 1.86 26-May-2005 kettenis

Implement PT_STEP for hppa.
ok mickey@


# 1.85 07-Apr-2005 mickey

dig lpa


Revision tags: OPENBSD_3_7_BASE
# 1.84 17-Jan-2005 mickey

proper fault address selection (list all possible pc-based traps)


# 1.83 17-Jan-2005 mickey

proper non-access fault handlers and probe emulations; testing by pval@ miod@ deraadt@


# 1.82 06-Dec-2004 miod

Use uvm_grow() to account for stack growth, rather than home-grown code
or nothing.
Inspired by a similar recent change in NetBSD.


# 1.81 14-Sep-2004 mickey

poke and clean all exceptions regs and not just one


Revision tags: OPENBSD_3_6_BASE
# 1.80 13-Jul-2004 mickey

more coherent intrs counting


# 1.79 02-Jul-2004 mickey

fill out error info into ret* regs upon failed syscall return


# 1.78 17-Jun-2004 mickey

use local var copy of frame->tf_iir everywhere and fix one panic


# 1.77 16-Jun-2004 mickey

handle zero division for userland as well; from otto@


Revision tags: SMP_SYNC_A
# 1.76 10-Jun-2004 kettenis

Generate SIGTRAP for breakpoint instructions.
ok mickey@


Revision tags: SMP_SYNC_B
# 1.75 13-May-2004 mickey

implement spop1,,0 support


# 1.74 12-May-2004 mickey

support probe instructions


# 1.73 21-Apr-2004 mickey

only set child return values in the child_return() more like other archs


# 1.72 07-Apr-2004 mickey

update copyright; miod@ is fine w/ files where he holds it too


# 1.71 02-Apr-2004 mickey

repair fcnv* emulation.
generate proper signals/siginfo.
partially repairs the "otto" case.


Revision tags: OPENBSD_3_5_BASE
# 1.70 20-Dec-2003 miod

Pass -Wformat, fix a few uninitialized variables as well.


# 1.69 23-Oct-2003 miod

Maybe fixing typos will unbore me.


Revision tags: OPENBSD_3_4_BASE
# 1.68 07-Aug-2003 mickey

do not treat traps from the syscall gate page as user code


# 1.67 26-Jul-2003 mickey

on syscall exit restore old cpl if there is a need; move call for softints processing towards after that


# 1.66 17-Jun-2003 jfb

typos

ok jmc@


Revision tags: UBC_SYNC_A
# 1.65 11-Apr-2003 mickey

always reread curproc and frame on return from the system call


# 1.64 11-Apr-2003 mickey

count traps and interrupts


# 1.63 07-Apr-2003 mickey

only check for space mismatch when coming from user.
the only case of user refering the kernel space is for the
syscall and that never faults since always mapped.
this fixes some random sig11 occurences.


Revision tags: OPENBSD_3_3_BASE
# 1.62 25-Feb-2003 miod

In trap(), always initialize tts, even if TRAPDEBUG is not defined, as it
might needed for panic() messages.
ok mickey@


# 1.61 18-Feb-2003 deraadt

knf; mickey ok


# 1.60 18-Feb-2003 miod

Let kernels compile without DDB, USELEDS, and DIAGNOSTIC.
For future intallation media.


# 1.59 22-Jan-2003 mickey

consistantly use uvm_prot_* vs vm_prot_* evewrhere


# 1.58 09-Jan-2003 mickey

on user trap space mismatch just kill the offender, panic the kernel otherwise


# 1.57 08-Jan-2003 mickey

make sure we are not allowing user to fault outside user address space and always print smth before dead_end


# 1.56 07-Jan-2003 mickey

kill some debugging kdb_traps


# 1.55 19-Dec-2002 mickey

make sure we are hitting user stack and not smth else here, make init show normal vsz in ps now


# 1.54 17-Dec-2002 mickey

real interrupts/spl framework.
tested on 712/* 715/100, 715/33 which main cpu/bus types.
miod@ ok


Revision tags: UBC_SYNC_B
# 1.53 18-Oct-2002 mickey

this is a precision architecture -- be more precise about fault types and in sigsegv deliveries; vm_ssize is in pages, apparently


# 1.52 07-Oct-2002 mickey

on implementations w/ fpu included unimplemented instructions
are signaled through the exception trap w/ invalid opcode marked
instruction in the exception registers, not through the emulation
trap (as long as the fpu is enabled, of course).
parse emulation from the exception trap as well as the emulation
trap and fix the dispatcher into usable condition.
parse invalid op exception on trap and signal the user appropriately.
reset the exception on exec and for child on fork.
the later is appropriate since exceptions are delayed until next
fpu instruction, which was in the parent indeed, let him get it.
save parent's fpu context on fork before cipying it, if the
parent owned the fpu.


Revision tags: OPENBSD_3_2_BASE
# 1.51 23-Sep-2002 mickey

make the leds blink (on those machines where we have 'em).
simple logic is to light up the led in the intrhook and dim in the
heartbeat always, makes a believable impression.
upper four bits represent a cpu usage w/in the last Hz/8
made up from the cp_time[] times.


# 1.50 17-Sep-2002 mickey

handle fpu exceptions properly, might use a regress, i guess


# 1.49 12-Sep-2002 mickey

give a condition trap own treatment, per millicode; and, oh horror, swap return of quad_t, sort of, sometimes


# 1.48 11-Sep-2002 mickey

correct argsize for ktrsyscall


# 1.47 10-Sep-2002 mickey

copy syscall args in the syscall() based on the
amount given in the sysent.
deal w/ the words swappage phenomena due to reverse
long word storage on the stack and that being
reverse copied by words yielding long word being word swapped.
do it only for syscall(2) and __syscall(2) since that's how those
w/ long word args syscalls get used (for proper alignment).
discussed and ideas from: deraadt@, drahn@ and fredette@

note: this might be a good point for a regress.


# 1.46 13-Aug-2002 mickey

reset iir in the frame on _onfault handling, for mr.ddb


# 1.45 25-Jul-2002 mickey

only one trap generates exec fault and fetches the fault address from the pc queues, also supply fault_type (not that it is used that much)


# 1.44 21-Jul-2002 mickey

on itlb miss say type is VM_PROT_EXECUTE, per art@s suggestion


# 1.43 20-May-2002 mickey

force alignment on the netisr and sir by mobving 'em into .data
and allocating space in the locore (ldcw require a 16byte alignment).
inform gcc that ldcw will change the memory (in asm()).
fix spl handling in the soft intrs piece.


# 1.42 16-May-2002 miod

Add systrace support to all the remaining architectures.

Tested by various people on various platforms, I'm willing to fix any
breakage this causes.

ok niels@ deraadt@ and mickey@ (after his comments were applied)


Revision tags: OPENBSD_3_1_BASE
# 1.41 16-Mar-2002 mickey

separate out interrupt handling stuff


# 1.40 15-Mar-2002 mickey

rewrite a pmap to use multilevel page tables.
lower 12 bits contain the perms, no unused bits left,
but a couple for off-tlb use (as the ref implemented now).
do not use the hvt, which might get some use later
if proven to speed thigs up, tlb handlers would po
another dozen of insns though, but if that's worth its...
move on the data seg and map kernel text rdonly (idea form fredette),
since all of the page0 mods done before that we are all fine
except for some viper fluff, but later w/ that.
this also picks up a bit more of ddb magic for bpt and ss.
tlb handlers can use a little bit more of attention,
but things, visually, seem to be much faster already, --
sorry, no benchmarks for now.

* effort sponsored in part by the `henry st. old ale house'
* and mr.pete and mr.lee in particular in thier generous entrirety.
* the proj took a little more that 72man*h as it was expected,
* but within murhy's law estimations.


# 1.39 14-Mar-2002 millert

First round of __P removal in sys


# 1.38 07-Feb-2002 mickey

remove debugging printf


# 1.37 06-Feb-2002 mickey

disable trapdebug, better handling of eirr too


# 1.36 05-Feb-2002 mickey

use splraise() when calling handlers; from fredette@


# 1.35 02-Feb-2002 mickey

tiny fixens from netbsd


Revision tags: UBC_BASE
# 1.34 28-Nov-2001 art

branches: 1.34.2;
zap some typedefs.
vm_map_t -> struct vm_map *
vm_map_entry_t -> struct vm_map_entry *
simple_lock_data_t -> struct simplelock

(uvm not done yet, coming in the next commit)


# 1.33 28-Nov-2001 art

Sync in more uvm changes from NetBSD.
This time we're getting rid of KERN_* and VM_PAGER_* error codes and
use errnos instead.


# 1.32 06-Nov-2001 mickey

child_return unscrewart


# 1.31 06-Nov-2001 miod

Replace inclusion of <vm/foo.h> with the correct <uvm/bar.h> when necessary.
(Look ma, I might have broken the tree)


Revision tags: OPENBSD_3_0_BASE
# 1.30 19-Sep-2001 mickey

merge vm/vm_kern.h into uvm/uvm_extern.h; art@ ok


# 1.29 14-Sep-2001 art

simplify userret


# 1.28 05-May-2001 art

Get rid of CLSIZE and all related stuff.
CLSIZE -> 1
CLBYTES -> PAGE_SIZE
OLOFSET -> PAGE_MASK
etc.
At the same time some archs needed some cleaning in vmparam.h so that
goes in at the same time.


Revision tags: OPENBSD_2_9_BASE
# 1.27 01-Apr-2001 mickey

remove a little debugging


# 1.26 22-Mar-2001 mickey

handle d/i prot traps proper


# 1.25 29-Jan-2001 mickey

implement a few macros in cpu.h; pointer out by miod@


# 1.24 22-Jan-2001 mickey

dirty trap should be handed w/ uvm_fault() as well


# 1.23 10-Nov-2000 art

Change the ktrace interface functions from taking the trace vnode to taking the
traced proc. The vnode is in the proc and all functions need the proc.


# 1.22 08-Nov-2000 mickey

use net/netisr_dispatch.h


Revision tags: OPENBSD_2_8_BASE
# 1.21 05-Jul-2000 mickey

missing semicolon


# 1.20 08-Jun-2000 niklas

Add explicit inclusions of signalvar.h to files actually using syms defined
there but relying on an indirect inclusion


Revision tags: OPENBSD_2_7_BASE SMP_BASE
# 1.19 10-Feb-2000 mickey

branches: 1.19.2;
mark interrupt frames


# 1.18 25-Jan-2000 mickey

some more cautions and debuggings; repair __syscall


# 1.17 17-Jan-2000 mickey

some more paranoid checks, shorter trap desriptions


# 1.16 12-Jan-2000 mickey

choose right map for a fault, better pcq advancement technique


# 1.15 11-Jan-2000 mickey

rearrange diagnostic trap cases


# 1.14 31-Dec-1999 mickey

fix order of uvm_fault() arguments in debug printf (;


# 1.13 17-Dec-1999 mickey

track the maximum accessed stack size; basically this small piece of code is the same in all the archs


# 1.12 08-Dec-1999 mickey

s/ipv6intr/ip6intr/


Revision tags: kame_19991208
# 1.11 25-Nov-1999 mickey

many traps, syscall nad other stuff


Revision tags: OPENBSD_2_6_BASE
# 1.10 16-Aug-1999 mickey

report unhandled interrupts w/ DEBUG only


# 1.9 16-Aug-1999 mickey

unscrew the screup


# 1.8 16-Aug-1999 mickey

evcnt count the interrupts, also optimize diagnostic printing


# 1.7 14-Aug-1999 mickey

do the clock interrupt the narrow way.
fix soft interrupts.
some more debugs and other fixes.


# 1.6 21-Jul-1999 mickey

skip break insn in the proper place, print cr28 in ddb, some debugging cruft


# 1.5 16-Jul-1999 mickey

almost all of them


# 1.4 03-May-1999 mickey

child_return(), some more traps to handle


# 1.3 20-Apr-1999 mickey

syscall(), fix interrupt handling


Revision tags: OPENBSD_2_5_BASE
# 1.2 11-Jan-1999 millert

panic prints a newline for you, don't do it in the panic string


# 1.1 01-Jan-1999 mickey

some traps, hehe