1*388550b0Srillig /* $NetBSD: rumpuser_int.h,v 1.11 2022/04/19 20:32:17 rillig Exp $ */
26e4a9f91Spooka
36e4a9f91Spooka /*
46e4a9f91Spooka * Copyright (c) 2008 Antti Kantee. All Rights Reserved.
56e4a9f91Spooka *
66e4a9f91Spooka * Redistribution and use in source and binary forms, with or without
76e4a9f91Spooka * modification, are permitted provided that the following conditions
86e4a9f91Spooka * are met:
96e4a9f91Spooka * 1. Redistributions of source code must retain the above copyright
106e4a9f91Spooka * notice, this list of conditions and the following disclaimer.
116e4a9f91Spooka * 2. Redistributions in binary form must reproduce the above copyright
126e4a9f91Spooka * notice, this list of conditions and the following disclaimer in the
136e4a9f91Spooka * documentation and/or other materials provided with the distribution.
146e4a9f91Spooka *
156e4a9f91Spooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
166e4a9f91Spooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
176e4a9f91Spooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
186e4a9f91Spooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
196e4a9f91Spooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
206e4a9f91Spooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
216e4a9f91Spooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
226e4a9f91Spooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
236e4a9f91Spooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
246e4a9f91Spooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
256e4a9f91Spooka * SUCH DAMAGE.
266e4a9f91Spooka */
276e4a9f91Spooka
28b1b78627Spooka #include <stdlib.h>
29b1b78627Spooka
306e4a9f91Spooka #include <rump/rumpuser.h>
316e4a9f91Spooka
32*388550b0Srillig #define seterror(value) do { if (error) *error = value;} while (0)
33e9e1ac53Spooka
345af3856aSpooka extern struct rumpuser_hyperup rumpuser__hyp;
355af3856aSpooka
365af3856aSpooka static inline void
rumpkern_unsched(int * nlocks,void * interlock)375af3856aSpooka rumpkern_unsched(int *nlocks, void *interlock)
385af3856aSpooka {
395af3856aSpooka
405af3856aSpooka rumpuser__hyp.hyp_backend_unschedule(0, nlocks, interlock);
415af3856aSpooka }
425af3856aSpooka
435af3856aSpooka static inline void
rumpkern_sched(int nlocks,void * interlock)445af3856aSpooka rumpkern_sched(int nlocks, void *interlock)
455af3856aSpooka {
465af3856aSpooka
475af3856aSpooka rumpuser__hyp.hyp_backend_schedule(nlocks, interlock);
485af3856aSpooka }
495af3856aSpooka
506e4a9f91Spooka #define KLOCK_WRAP(a) \
516e4a9f91Spooka do { \
526e4a9f91Spooka int nlocks; \
535af3856aSpooka rumpkern_unsched(&nlocks, NULL); \
546e4a9f91Spooka a; \
555af3856aSpooka rumpkern_sched(nlocks, NULL); \
56*388550b0Srillig } while (0)
576e4a9f91Spooka
586e4a9f91Spooka #define DOCALL(rvtype, call) \
5910194743Spooka { \
606e4a9f91Spooka rvtype rv; \
616e4a9f91Spooka rv = call; \
626e4a9f91Spooka if (rv == -1) \
63e9e1ac53Spooka seterror(errno); \
646e4a9f91Spooka else \
65e9e1ac53Spooka seterror(0); \
6610194743Spooka return rv; \
6710194743Spooka }
686e4a9f91Spooka
696e4a9f91Spooka #define DOCALL_KLOCK(rvtype, call) \
7010194743Spooka { \
716e4a9f91Spooka rvtype rv; \
726e4a9f91Spooka int nlocks; \
735af3856aSpooka rumpkern_unsched(&nlocks, NULL); \
746e4a9f91Spooka rv = call; \
755af3856aSpooka rumpkern_sched(nlocks, NULL); \
766e4a9f91Spooka if (rv == -1) \
77e9e1ac53Spooka seterror(errno); \
786e4a9f91Spooka else \
79e9e1ac53Spooka seterror(0); \
8010194743Spooka return rv; \
8110194743Spooka }
8293b95ff6Spooka
8393b95ff6Spooka void rumpuser__thrinit(void);
84262a3aafSpooka
85*388550b0Srillig #define NOFAIL(a) do {if (!(a)) abort();} while (0)
86262a3aafSpooka
87262a3aafSpooka #define NOFAIL_ERRNO(a) \
88262a3aafSpooka do { \
89262a3aafSpooka int fail_rv = (a); \
90262a3aafSpooka if (fail_rv) { \
91262a3aafSpooka printf("panic: rumpuser fatal failure %d (%s)\n", \
92262a3aafSpooka fail_rv, strerror(fail_rv)); \
93262a3aafSpooka abort(); \
94262a3aafSpooka } \
95*388550b0Srillig } while (0)
96be085dcdSpooka
97613311d8Spooka int rumpuser__sig_rump2host(int);
98be085dcdSpooka int rumpuser__errtrans(int);
99be085dcdSpooka #ifdef __NetBSD__
100be085dcdSpooka #define ET(_v_) return (_v_);
101be085dcdSpooka #else
102be085dcdSpooka #define ET(_v_) return (_v_) ? rumpuser__errtrans(_v_) : 0;
103be085dcdSpooka #endif
104648d66f0Sjustin
105648d66f0Sjustin int rumpuser__random_init(void);
106