+int mmupid = -1;
+#define MDB_FOLLOW 1
+#define MDB_WBFOLLOW 2
+#define MDB_WBFAILED 4
+#define MDB_ISPID(p) (p) == mmupid
+#endif
+
+/*
+ * trap and syscall both need the following work done before returning
+ * to user mode.
+ */
+static inline void
+userret(p, fp, oticks, faultaddr, fromtrap)
+ register struct proc *p;
+ register struct frame *fp;
+ u_quad_t oticks;
+ u_int faultaddr;
+ int fromtrap;
+{
+ int sig, s;
+#ifdef HP380
+ int beenhere = 0;
+
+again:
+#endif
+ /* take pending signals */
+ while ((sig = CURSIG(p)) != 0)
+ postsig(sig);
+ p->p_priority = p->p_usrpri;
+ if (want_resched) {
+ /*
+ * Since we are curproc, clock will normally just change
+ * our priority without moving us from one queue to another
+ * (since the running process is not on a queue.)
+ * If that happened after we put ourselves on the run queue
+ * but before we mi_switch()'ed, we might not be on the queue
+ * indicated by our priority.
+ */
+ s = splstatclock();
+ setrunqueue(p);
+ p->p_stats->p_ru.ru_nivcsw++;
+ mi_switch();
+ splx(s);
+ while ((sig = CURSIG(p)) != 0)
+ postsig(sig);
+ }
+
+ /*
+ * If profiling, charge system time to the trapped pc.
+ */
+ if (p->p_flag & P_PROFIL) {
+ extern int psratio;
+
+ addupc_task(p, fp->f_pc,
+ (int)(p->p_sticks - oticks) * psratio);
+ }
+#ifdef HP380
+ /*
+ * Deal with user mode writebacks (from trap, or from sigreturn).
+ * If any writeback fails, go back and attempt signal delivery.
+ * unless we have already been here and attempted the writeback
+ * (e.g. bad address with user ignoring SIGSEGV). In that case
+ * we just return to the user without sucessfully completing
+ * the writebacks. Maybe we should just drop the sucker?
+ */
+ if (mmutype == MMU_68040 && fp->f_format == FMT7) {
+ if (beenhere) {
+#ifdef DEBUG
+ if (mmudebug & MDB_WBFAILED)
+ printf(fromtrap ?
+ "pid %d(%s): writeback aborted, pc=%x, fa=%x\n" :
+ "pid %d(%s): writeback aborted in sigreturn, pc=%x\n",
+ p->p_pid, p->p_comm, fp->f_pc, faultaddr);
+#endif
+ } else if (sig = writeback(fp, fromtrap)) {
+ beenhere = 1;
+ oticks = p->p_sticks;
+ trapsignal(p, sig, faultaddr);
+ goto again;
+ }
+ }