summaryrefslogtreecommitdiffstats
path: root/pk/kernel/pk_macros.h
blob: 45bfbac611d470d6b0c486293090cd2ca3e640de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#ifndef __PK_MACROS_H__
#define __PK_MACROS_H__
//-----------------------------------------------------------------------------
// *! (C) Copyright International Business Machines Corp. 2014
// *! All Rights Reserved -- Property of IBM
// *! *** IBM Confidential ***
//-----------------------------------------------------------------------------

/// \file pk_macros.h
/// \brief Boilerplate macros for PK

/// This macro encapsulates error handling boilerplate for code that uses the
/// PK API-type error handling, for errors that do not occur in critical
/// sections.

#define PK_ERROR(code) \
    do { \
        if (PK_ERROR_PANIC) {                  \
            PK_PANIC(code);                    \
        } else {                                \
            return -(code);                     \
        }                                       \
    } while (0)


/// This macro encapsulates error handling boilerplate in the PK API
/// functions, for errors that do not occur in critical sections.

#define PK_ERROR_IF(condition, code) \
    do { \
        if (condition) {                        \
            PK_ERROR(code);                    \
        }                                       \
    } while (0)


/// This macro encapsulates error handling boilerplate in the PK API
/// functions, for errors that do not occur in critical sections and always
/// force a kernel panic, indicating a kernel or API bug.

#define PK_PANIC_IF(condition, code)           \
    do {                                        \
        if (condition) {                        \
            PK_PANIC(code);                    \
        }                                       \
    } while (0)


/// This macro encapsulates error handling boilerplate in the PK API
/// functions, for errors that do not occur in critical sections.
/// The error handling will only be enabled when PK_ERROR_CHECK_API 
/// is enabled.

#define PK_ERROR_IF_CHECK_API(condition, code) \
    do { \
        if (PK_ERROR_CHECK_API) {              \
            PK_ERROR_IF(condition, code);       \
        }                                       \
    } while (0)

/// This macro encapsulates error handling boilerplate in the PK API
/// functions, for errors that occur in critical sections.

#define PK_ERROR_IF_CRITICAL(condition, code, context) \
    do { \
        if (condition) {                                \
            if (PK_ERROR_PANIC) {                      \
                PK_PANIC(code);                        \
                pk_critical_section_exit(context);     \
            } else {                                    \
                pk_critical_section_exit(context);     \
                return -(code);                         \
            }                                           \
        }                                               \
    } while (0)


/// This is a general macro for errors that require cleanup before returning
/// the error code.

#define PK_ERROR_IF_CLEANUP(condition, code, cleanup) \
    do { \
        if (condition) {                        \
            if (PK_ERROR_PANIC) {              \
                PK_PANIC(code);                \
                cleanup;                        \
            } else {                            \
                cleanup;                        \
                return -(code);                 \
            }                                   \
        }                                       \
    } while (0)



/// Some PK APIs can only be called from thread contexts - these are APIs
/// that threads call on 'themselves'.

#define PK_ERROR_UNLESS_THREAD_CONTEXT() \
    PK_ERROR_IF(!__pk_kernel_context_thread(), \
                 PK_ILLEGAL_CONTEXT_THREAD_CONTEXT)


/// Some PK APIs must be called from an interrupt context only.

#define PK_ERROR_UNLESS_ANY_INTERRUPT_CONTEXT() \
    PK_ERROR_IF(!__pk_kernel_context_any_interrupt(), \
                 PK_ILLEGAL_CONTEXT_INTERRUPT_CONTEXT)

#endif /* __PK_MACROS_H__ */
OpenPOWER on IntegriCloud