vm: fix a null dereference on out-of-memory
[minix.git] / lib / libc / arch / m68k / gen / longjmp.c
blobe8d5d038790cb3737e7679c8188f456e7df12727
1 /* $NetBSD: longjmp.c,v 1.2 2008/04/28 20:22:56 martin Exp $ */
3 /*-
4 * Copyright (c) 2003 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Christian Limpach.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
32 #include "namespace.h"
33 #include <sys/types.h>
34 #include <ucontext.h>
35 #include <signal.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #define __LIBC12_SOURCE__
40 #include <setjmp.h>
41 #include <compat/include/setjmp.h>
43 typedef struct {
44 __greg_t __data[6];
45 __greg_t __addr[4];
46 } __jmp_buf_regs_t;
48 void
49 __longjmp14(jmp_buf env, int val)
51 struct sigcontext *sc = (void *)env;
52 __jmp_buf_regs_t *r = (void *)&sc[1];
53 ucontext_t uc;
55 /* Ensure non-zero SP */
56 if (sc->sc_sp == 0)
57 goto err;
59 /* Make return value non-zero */
60 if (val == 0)
61 val = 1;
63 /* Save return value in context */
64 uc.uc_mcontext.__gregs[_REG_D0] = val;
67 * Set _UC_SIGMASK, _UC_CPU and _UC_M68K_UC_USER
68 * Set _UC_{SET,CLR}STACK according to SS_ONSTACK
70 uc.uc_flags = _UC_SIGMASK | _UC_CPU | _UC_M68K_UC_USER |
71 (sc->sc_onstack ? _UC_SETSTACK : _UC_CLRSTACK);
73 /* Clear uc_link */
74 uc.uc_link = 0;
76 /* Copy signal mask */
77 uc.uc_sigmask = sc->sc_mask;
79 /* Copy SP/PC/PS/FP */
80 uc.uc_mcontext.__gregs[_REG_A7] = sc->sc_sp;
81 uc.uc_mcontext.__gregs[_REG_PC] = sc->sc_pc;
82 uc.uc_mcontext.__gregs[_REG_PS] = sc->sc_ps;
83 uc.uc_mcontext.__gregs[_REG_A6] = sc->sc_fp;
85 /* Copy remaining non-scratch regs. */
86 memcpy(&uc.uc_mcontext.__gregs[_REG_D2],
87 &r->__data, sizeof(r->__data));
88 memcpy(&uc.uc_mcontext.__gregs[_REG_A2],
89 &r->__addr, sizeof(r->__addr));
91 setcontext(&uc);
92 err:
93 longjmperror();
94 abort();
95 /* NOTREACHED */