IBNOS
util.c
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 #include <util/util.h>
29 #include <console/console.h>
30 
37 uint32_t stringLength(const char *str)
38 {
39  uint32_t len = 0;
40  if (!str)
41  return 0;
42 
43  while (*str++)
44  len++;
45 
46  return len;
47 }
48 
57 bool stringIsEqual(const char *str, const char *buf, uint32_t len)
58 {
59  /* remove terminating nulls from buf */
60  while (len && !buf[len - 1]) len--;
61 
62  if (!str)
63  return (len == 0);
64 
65  while (*str && len && (*str == *buf))
66  {
67  str++;
68  buf++;
69  len--;
70  }
71 
72  /* both strings are equal if this was the last character */
73  return (!len && !*str);
74 }
75 
84 uint32_t stringParseOctal(const char *str, uint32_t len)
85 {
86  uint32_t value = 0;
87 
88  /* skip over leading whitespace */
89  while (len && *str == ' ')
90  {
91  str++;
92  len--;
93  }
94 
95  while (len && (*str >= '0' && *str < '8'))
96  {
97  value = (value << 3) | (*str - '0');
98  str++;
99  len--;
100  }
101 
102  /* skip over trailing whitespace */
103  while (len && *str == ' ')
104  {
105  str++;
106  len--;
107  }
108 
109  /* not a nulltermination */
110  if (len && *str) return -1;
111 
112  return value;
113 }
114 
123 void *memset(void *ptr, int value, size_t num)
124 {
125  uint8_t *dst = ptr;
126  while (num--) *dst++ = value;
127  return ptr;
128 }
129 
141 void *memcpy(void *destination, const void *source, size_t num)
142 {
143  uint8_t *dst = destination;
144  const uint8_t *src = source;
145  while (num--) *dst++ = *src++;
146  return destination;
147 }
148 
159 void *memmove(void *destination, const void *source, size_t num)
160 {
161  if (destination < source || destination >= source + num)
162  return memcpy(destination, source, num);
163  else if (destination != source)
164  {
165  uint8_t *dst = (uint8_t *)destination + num - 1;
166  uint8_t *src = (uint8_t *)source + num - 1;
167  while (num--) *dst-- = *src--;
168  }
169  return destination;
170 }
171 
182 void debugCaptureCpuContext(struct taskContext *context);
183 asm(".text\n.align 4\n"
184 ".globl debugCaptureCpuContext\n"
185 "debugCaptureCpuContext:\n"
186 " pushl %eax\n"
187 " movl 8(%esp), %eax\n"
188 "\n"
189 " popl 0x28(%eax)\n"
190 " movl %ecx, 0x2C(%eax)\n"
191 " movl %edx, 0x30(%eax)\n"
192 " movl %ebx, 0x34(%eax)\n"
193 " leal 4(%esp), %edx\n"
194 " movl %edx, 0x38(%eax)\n"
195 " movl %ebp, 0x3C(%eax)\n"
196 " movl %esi, 0x40(%eax)\n"
197 " movl %edi, 0x44(%eax)\n"
198 "\n"
199 " movw %es, 0x48(%eax)\n"
200 " movw %cs, 0x4C(%eax)\n"
201 " movw %ss, 0x50(%eax)\n"
202 " movw %ds, 0x54(%eax)\n"
203 " movw %fs, 0x58(%eax)\n"
204 " movw %gs, 0x5C(%eax)\n"
205 "\n"
206 " movl %cr3, %edx\n"
207 " movl %edx, 0x1C(%eax)\n"
208 " movl (%esp), %edx\n"
209 " movl %edx, 0x20(%eax)\n"
210 " pushfl\n"
211 " popl 0x24(%eax)\n"
212 "\n"
213 " ret\n"
214 );
215 
228 void debugAssertFailed(const char *assertion, const char *file, const char *function, const char *line, struct taskContext *context)
229 {
230  const char *lines[] =
231  {
232  " ASSERTION FAILED ",
233  " Assertion: ", assertion,
234  "\n File: ", file,
235  "\n Function: ", function,
236  "\n Line: ", line,
237  NULL
238  };
239 
240  consoleSystemFailure(lines, 0, NULL, context);
241 }
242 
251 void debugNotImplemented(const char *file, const char *function, const char *line, struct taskContext *context)
252 {
253  const char *lines[] =
254  {
255  " NOT IMPLEMENTED ",
256  " Unimplemented code section reached.",
257  "\n",
258  "\n File: ", file,
259  "\n Function: ", function,
260  "\n Line: ", line,
261  NULL
262  };
263 
264  consoleSystemFailure(lines, 0, NULL, context);
265 }
uint32_t stringLength(const char *str)
Returns the length of a nullterminated string.
Definition: util.c:37
void * memcpy(void *destination, const void *source, size_t num)
Copies a block of memory from source to destination.
Definition: util.c:141
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
void debugCaptureCpuContext(struct taskContext *context)
Fills out the task context structure with the values from the currently running code.
void * memmove(void *destination, const void *source, size_t num)
Moves a block of memory from source to destination.
Definition: util.c:159
void * memset(void *ptr, int value, size_t num)
Fills a memory region with some specific byte value.
Definition: util.c:123
uint32_t stringParseOctal(const char *str, uint32_t len)
Converts an octal string to a integer number.
Definition: util.c:84
void consoleSystemFailure(const char **lines, uint32_t numArgs, uint32_t *args, struct taskContext *context)
Print a system failure message and halts the system.
Definition: console.c:589
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 value
Definition: paging.h:55
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