[f761f1eb] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2001-2004 Jakub Jermar
|
---|
[f761f1eb] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
[b1d3c36] | 29 | /** @addtogroup generic
|
---|
[b45c443] | 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | */
|
---|
| 34 |
|
---|
[06e1e95] | 35 | #ifndef KERN_CONTEXT_H_
|
---|
| 36 | #define KERN_CONTEXT_H_
|
---|
[f761f1eb] | 37 |
|
---|
[d5d2a3f] | 38 | #include <arch/types.h>
|
---|
[4b2c872d] | 39 | #include <arch/context.h>
|
---|
| 40 |
|
---|
[b1d3c36] | 41 | #define context_set_generic(ctx, _pc, stack, size) \
|
---|
| 42 | (ctx)->pc = (uintptr_t) (_pc); \
|
---|
| 43 | (ctx)->sp = ((uintptr_t) (stack)) + (size) - SP_DELTA;
|
---|
[0ca6faa] | 44 |
|
---|
[b1d3c36] | 45 | extern int context_save_arch(context_t *ctx) __attribute__((returns_twice));
|
---|
| 46 | extern void context_restore_arch(context_t *ctx) __attribute__((noreturn));
|
---|
[4b2c872d] | 47 |
|
---|
| 48 | /** Save register context.
|
---|
| 49 | *
|
---|
[b1c21c2] | 50 | * Save the current register context (including stack pointer) to a context
|
---|
| 51 | * structure. A subsequent call to context_restore() will return to the same
|
---|
[4b2c872d] | 52 | * address as the corresponding call to context_save().
|
---|
| 53 | *
|
---|
[b1c21c2] | 54 | * Note that context_save_arch() must reuse the stack frame of the function
|
---|
| 55 | * which called context_save(). We guarantee this by:
|
---|
| 56 | *
|
---|
| 57 | * a) implementing context_save_arch() in assembly so that it does not create
|
---|
| 58 | * its own stack frame, and by
|
---|
| 59 | * b) defining context_save() as a macro because the inline keyword is just a
|
---|
| 60 | * hint for the compiler, not a real constraint; the application of a macro
|
---|
| 61 | * will definitely not create a stack frame either.
|
---|
| 62 | *
|
---|
| 63 | * To imagine what could happen if there were some extra stack frames created
|
---|
| 64 | * either by context_save() or context_save_arch(), we need to realize that the
|
---|
| 65 | * sp saved in the contex_t structure points to the current stack frame as it
|
---|
| 66 | * existed when context_save_arch() was executing. After the return from
|
---|
| 67 | * context_save_arch() and context_save(), any extra stack frames created by
|
---|
| 68 | * these functions will be destroyed and their contents sooner or later
|
---|
| 69 | * overwritten by functions called next. Any attempt to restore to a context
|
---|
| 70 | * saved like that would therefore lead to a disaster.
|
---|
[e1888f9] | 71 | *
|
---|
[b1d3c36] | 72 | * @param ctx Context structure.
|
---|
| 73 | *
|
---|
| 74 | * @return context_save() returns 1, context_restore() returns 0.
|
---|
[4b2c872d] | 75 | *
|
---|
| 76 | */
|
---|
[b1d3c36] | 77 | #define context_save(ctx) context_save_arch(ctx)
|
---|
[4b2c872d] | 78 |
|
---|
| 79 | /** Restore register context.
|
---|
| 80 | *
|
---|
[b1c21c2] | 81 | * Restore a previously saved register context (including stack pointer) from
|
---|
| 82 | * a context structure.
|
---|
[4b2c872d] | 83 | *
|
---|
[b1c21c2] | 84 | * Note that this function does not normally return. Instead, it returns to the
|
---|
| 85 | * same address as the corresponding call to context_save(), the only difference
|
---|
| 86 | * being return value.
|
---|
[4b2c872d] | 87 | *
|
---|
[b1d3c36] | 88 | * @param ctx Context structure.
|
---|
[4b2c872d] | 89 | */
|
---|
[b1d3c36] | 90 | static inline void context_restore(context_t *ctx)
|
---|
[4b2c872d] | 91 | {
|
---|
[b1d3c36] | 92 | context_restore_arch(ctx);
|
---|
[4b2c872d] | 93 | }
|
---|
[f761f1eb] | 94 |
|
---|
| 95 | #endif
|
---|
[b45c443] | 96 |
|
---|
[42d3be3] | 97 | /** @}
|
---|
[b45c443] | 98 | */
|
---|