blob: 689bf01b687a75b1c6d2e52523a59880d7704d48 [file] [log] [blame]
Patrick Georgi980a69b2010-06-24 11:16:10 +00001/*
2 * This file is part of the libpayload project.
3 *
4 * Copyright (C) 2008 Advanced Micro Devices, Inc.
Gabe Blackfa738752013-11-23 00:54:40 -08005 * Copyright 2013 Google Inc.
Patrick Georgi980a69b2010-06-24 11:16:10 +00006 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31#ifndef _STDLIB_H
32#define _STDLIB_H
33
Gabe Blackfa738752013-11-23 00:54:40 -080034#include <die.h>
Patrick Georgi980a69b2010-06-24 11:16:10 +000035#include <stddef.h>
Gabe Blackfa738752013-11-23 00:54:40 -080036#include <string.h>
Patrick Georgi980a69b2010-06-24 11:16:10 +000037
Daisuke Nojiri03411692014-03-05 15:46:28 -080038#define ALIGN(x,a) __ALIGN_MASK(x,(typeof(x))(a)-1UL)
39#define __ALIGN_MASK(x,mask) (((x)+(mask))&~(mask))
40#define ALIGN_UP(x,a) ALIGN((x),(a))
41#define ALIGN_DOWN(x,a) ((x) & ~((typeof(x))(a)-1UL))
Jimmy Huang0fd3e792015-04-13 20:28:38 +080042#define IS_ALIGNED(x,a) (((x) & ((typeof(x))(a)-1UL)) == 0)
Daisuke Nojiri03411692014-03-05 15:46:28 -080043
Patrick Georgi980a69b2010-06-24 11:16:10 +000044/**
45 * @defgroup malloc Memory allocation functions
46 * @{
47 */
Stefan Reinauer1b4d3942015-06-29 15:47:34 -070048#if IS_ENABLED(CONFIG_LP_DEBUG_MALLOC) && !defined(IN_MALLOC_C)
Patrick Georgi980a69b2010-06-24 11:16:10 +000049#define free(p) \
50 ({ \
51 extern void print_malloc_map(void); \
52 extern void free(void *); \
53 printf("free(%p) called from %s:%s:%d...\n", p, __FILE__, __func__, \
54 __LINE__);\
55 printf("PRE free()\n"); \
56 print_malloc_map(); \
57 free(p); \
58 printf("POST free()\n"); \
59 print_malloc_map(); \
60 })
61#define malloc(s) \
62 ({ \
63 extern void print_malloc_map(void); \
64 extern void *malloc(size_t); \
65 void *ptr; \
66 printf("malloc(%u) called from %s:%s:%d...\n", s, __FILE__, __func__, \
67 __LINE__);\
68 printf("PRE malloc\n"); \
69 print_malloc_map(); \
70 ptr = malloc(s); \
71 printf("POST malloc (ptr = %p)\n", ptr); \
72 print_malloc_map(); \
73 ptr; \
74 })
75#define calloc(n,s) \
76 ({ \
77 extern void print_malloc_map(void); \
78 extern void *calloc(size_t,size_t); \
79 void *ptr; \
80 printf("calloc(%u, %u) called from %s:%s:%d...\n", n, s, __FILE__, \
81 __func__, __LINE__);\
82 printf("PRE calloc\n"); \
83 print_malloc_map(); \
84 ptr = calloc(n,s); \
85 printf("POST calloc (ptr = %p)\n", ptr); \
86 print_malloc_map(); \
87 ptr; \
88 })
89#define realloc(p,s) \
90 ({ \
91 extern void print_malloc_map(void); \
92 extern void *realloc(void*,size_t); \
93 void *ptr; \
94 printf("realloc(%p, %u) called from %s:%s:%d...\n", p, s, __FILE__, \
95 __func__, __LINE__);\
96 printf("PRE realloc\n"); \
97 print_malloc_map(); \
98 ptr = realloc(p,s); \
99 printf("POST realloc (ptr = %p)\n", ptr); \
100 print_malloc_map(); \
101 ptr; \
102 })
103#define memalign(a,s) \
104 ({ \
105 extern void print_malloc_map(void); \
106 extern void *memalign(size_t, size_t); \
107 void *ptr; \
108 printf("memalign(%u, %u) called from %s:%s:%d...\n", a, s, __FILE__, \
109 __func__, __LINE__);\
110 printf("PRE memalign\n"); \
111 print_malloc_map(); \
112 ptr = memalign(a,s); \
Julius Werner9665d382013-09-13 18:21:46 -0700113 printf("POST memalign (ptr = %p)\n", ptr); \
114 print_malloc_map(); \
115 ptr; \
116 })
117#define dma_malloc(s) \
118 ({ \
119 extern void print_malloc_map(void); \
120 extern void *dma_malloc(size_t); \
121 void *ptr; \
122 printf("dma_malloc(%u) called from %s:%s:%d...\n", s, __FILE__, \
123 __func__, __LINE__);\
124 printf("PRE dma_malloc\n"); \
125 print_malloc_map(); \
126 ptr = dma_malloc(s); \
127 printf("POST dma_malloc (ptr = %p)\n", ptr); \
128 print_malloc_map(); \
129 ptr; \
130 })
131#define dma_memalign(a,s) \
132 ({ \
133 extern void print_malloc_map(void); \
134 extern void *dma_memalign(size_t, size_t); \
135 void *ptr; \
136 printf("dma_memalign(%u, %u) called from %s:%s:%d...\n", a, s, \
137 __FILE__, __func__, __LINE__);\
138 printf("PRE dma_memalign\n"); \
139 print_malloc_map(); \
140 ptr = dma_memalign(a,s); \
141 printf("POST dma_memalign (ptr = %p)\n", ptr); \
Patrick Georgi980a69b2010-06-24 11:16:10 +0000142 print_malloc_map(); \
143 ptr; \
144 })
145#else
146void free(void *ptr);
147void *malloc(size_t size);
148void *calloc(size_t nmemb, size_t size);
149void *realloc(void *ptr, size_t size);
150void *memalign(size_t align, size_t size);
Julius Wernerb8fad3d2013-08-27 15:48:32 -0700151void *dma_malloc(size_t size);
152void *dma_memalign(size_t align, size_t size);
Julius Werner9665d382013-09-13 18:21:46 -0700153#endif
154void init_dma_memory(void *start, u32 size);
Julius Werner509c37e2013-08-28 12:29:28 -0700155int dma_initialized(void);
156int dma_coherent(void *ptr);
Gabe Blackfa738752013-11-23 00:54:40 -0800157
158static inline void *xmalloc_work(size_t size, const char *file,
159 const char *func, int line)
160{
161 void *ret = malloc(size);
162 if (!ret && size) {
163 die_work(file, func, line, "Failed to malloc %zu bytes.\n",
164 size);
165 }
166 return ret;
167}
168#define xmalloc(size) xmalloc_work((size), __FILE__, __FUNCTION__, __LINE__)
169
170static inline void *xzalloc_work(size_t size, const char *file,
171 const char *func, int line)
172{
173 void *ret = xmalloc_work(size, file, func, line);
174 memset(ret, 0, size);
175 return ret;
176}
177#define xzalloc(size) xzalloc_work((size), __FILE__, __FUNCTION__, __LINE__)
Aaron Durbinc88cca12015-01-22 08:53:02 -0600178
179static inline void *xmemalign_work(size_t align, size_t size, const char *file,
180 const char *func, int line)
181{
182 void *ret = memalign(align, size);
183 if (!ret && size) {
184 die_work(file, func, line,
185 "Failed to memalign %zu bytes with %zu alignment.\n",
186 size, align);
187 }
188 return ret;
189}
190#define xmemalign(align, size) \
191 xmemalign_work((align), (size), __FILE__, __func__, __LINE__)
Patrick Georgi980a69b2010-06-24 11:16:10 +0000192/** @} */
193
194/**
195 * @defgroup stdlib String conversion functions
196 * @{
197 */
198long int strtol(const char *s, char **nptr, int base);
199unsigned long int strtoul(const char *s, char **nptr, int base);
Patrick Georgic643fdd2011-07-26 12:51:59 +0200200unsigned long long int strtoull(const char *s, char **nptr, int base);
Patrick Georgic3c827c2011-05-27 15:31:52 +0200201long atol(const char *nptr);
Patrick Georgi980a69b2010-06-24 11:16:10 +0000202
203/** @} */
204
205/**
206 * @defgroup rand Random number generator functions
207 * @{
208 */
209int rand_r(unsigned int *seed);
210int rand(void);
211void srand(unsigned int seed);
212/** @} */
213
214/**
215 * Stop execution and halt the processor (this function does not return).
216 */
217void halt(void) __attribute__ ((noreturn));
218void exit(int status) __attribute__ ((noreturn));
219#define abort() halt() /**< Alias for the halt() function */
Stefan Reinauer1b4d3942015-06-29 15:47:34 -0700220#if IS_ENABLED(CONFIG_LP_REMOTEGDB)
Julius Werner50a81742014-05-15 11:57:38 -0700221/* Override abort()/halt() to trap into GDB if it is enabled. */
222#define halt() do { gdb_enter(); halt(); } while (0)
223#endif
Patrick Georgi980a69b2010-06-24 11:16:10 +0000224
225/** @} */
226
Patrick Georgi537cacf2011-07-07 12:02:10 +0200227void qsort(void *aa, size_t n, size_t es, int (*cmp)(const void *, const void *));
Patrick Georgidd3e6902011-07-12 15:50:54 +0200228char *getenv(const char*);
Vadim Bendeburye63990e2014-11-27 18:50:14 -0800229uint64_t __umoddi3(uint64_t num, uint64_t den);
230uint64_t __udivdi3(uint64_t num, uint64_t den);
231uint64_t __ashldi3(uint64_t num, unsigned shift);
232uint64_t __lshrdi3(uint64_t num, unsigned shift);
233
Patrick Georgi980a69b2010-06-24 11:16:10 +0000234#endif