IBNOS
util.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, Michael Müller
3  * Copyright (c) 2014, Sebastian Lackner
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
19  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  */
27 
28 #ifndef _H_UTIL_
29 #define _H_UTIL_
30 
31 #ifdef __KERNEL__
32 
33  #include <stddef.h>
34  #include <stdint.h>
35  #include <stdbool.h>
36 
37  #include <hardware/context.h>
38 
39  #define UNUSED __attribute__((unused))
40 
41  uint32_t stringLength(const char *str);
42  bool stringIsEqual(const char *str, const char *buf, uint32_t len);
43  uint32_t stringParseOctal(const char *str, uint32_t len);
44 
45  void *memset(void *ptr, int value, size_t num);
46  void *memcpy(void *destination, const void *source, size_t num);
47  void *memmove(void *destination, const void *source, size_t num);
48 
49  void debugCaptureCpuContext(struct taskContext *context);
50  void debugAssertFailed(const char *assertion, const char *file, const char *function, const char *line, struct taskContext *context);
51  void debugNotImplemented(const char *file, const char *function, const char *line, struct taskContext *context);
52 
53  inline void debugHalt()
54  {
55  for (;;) asm volatile("cli\nhlt");
56  }
57 
58  #define _STRINGIZE_DETAIL(x) #x
59  #define _STRINGIZE(x) _STRINGIZE_DETAIL(x)
60 
61  #define assert(ex) \
62  do \
63  { \
64  struct taskContext __context; \
65  if ((ex)) break; \
66  debugCaptureCpuContext(&__context); \
67  debugAssertFailed(#ex, __FILE__, __FUNCTION__, _STRINGIZE(__LINE__), &__context); \
68  } \
69  while(1)
70 
71  #define SYSTEM_FAILURE(lines, ...) \
72  do \
73  { \
74  struct taskContext __context; \
75  uint32_t __args[] = {__VA_ARGS__}; \
76  memset(&__context, 0xFF, sizeof(__context)); \
77  debugCaptureCpuContext(&__context); \
78  consoleSystemFailure((lines), sizeof(__args)/sizeof(__args[0]), __args, &__context); \
79  } \
80  while(1)
81 
82  #define NOTIMPLEMENTED() \
83  do \
84  { \
85  struct taskContext __context; \
86  debugCaptureCpuContext(&__context); \
87  debugNotImplemented(__FILE__, __FUNCTION__, _STRINGIZE(__LINE__), &__context); \
88  } \
89  while(1)
90 
91 #endif
92 
93 #endif /* _H_UTIL_ */
void debugHalt()
Definition: util.h:53
void * memset(void *ptr, int value, size_t num)
Fills a memory region with some specific byte value.
Definition: util.c:123
uint32_t stringLength(const char *str)
Returns the length of a nullterminated string.
Definition: util.c:37
void debugCaptureCpuContext(struct taskContext *context)
void * memmove(void *destination, const void *source, size_t num)
Moves a block of memory from source to destination.
Definition: util.c:159
void * memcpy(void *destination, const void *source, size_t num)
Copies a block of memory from source to destination.
Definition: util.c:141
void debugNotImplemented(const char *file, const char *function, const char *line, struct taskContext *context)
Used internally to implement NOTIMPLEMENTED()
Definition: util.c:251
uint32_t stringParseOctal(const char *str, uint32_t len)
Converts an octal string to a integer number.
Definition: util.c:84
void debugAssertFailed(const char *assertion, const char *file, const char *function, const char *line, struct taskContext *context)
Used internally to implement assert()
Definition: util.c:228
bool stringIsEqual(const char *str, const char *buf, uint32_t len)
Checks if the string is equal to the memory region.
Definition: util.c:57
uint32_t value
Definition: paging.h:55