summaryrefslogtreecommitdiff
path: root/common/vboot_stub.c
blob: b9b6bed691b1ec8a04b4f027c046aa3a1a5f7013 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/* Copyright (c) 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */

/* Functions needed by vboot library */

#define _STUB_IMPLEMENTATION_
#include "console.h"
#include "shared_mem.h"
#include "util.h"
#include "utility.h"

/* Console output macros */
#define CPUTS(outstr) cputs(CC_VBOOT, outstr)
#define CPRINTF(format, args...) cprintf(CC_VBOOT, format, ## args)

#if 0 /* change this to debug memory usage */
#define DPRINTF CPRINTF
#else
#define DPRINTF(...)
#endif


/****************************************************************************/

void *Memcpy(void *dest, const void *src, uint64_t n)
{
	return memcpy(dest, src, (size_t)n);
}

void *Memset(void *d, const uint8_t c, uint64_t n)
{
	return memset(d, c, n);
}

int Memcmp(const void *src1, const void *src2, size_t n)
{
	size_t i;
	const uint8_t *a = src1;
	const uint8_t *b = src2;
	for (i = 0; i < n; i++) {
		if (*a != *b)
			return (*a < *b) ? -1 : 1;
		a++;
		b++;
	}

	return 0;
}

/****************************************************************************/
/* The vboot_api library requires some dynamic memory, but we don't have
 * malloc/free. Instead we have one chunk of shared RAM that we can gain
 * exclusive access to for a time. We'll have to experiment with various
 * algorithms to see what works best. This algorithm allocates and
 * reuses blocks, but never actually frees anything until all memory has been
 * reclaimed. */

/* Since we only need this stuff to boot, don't waste run-time .bss */
static struct {
	int bucket_size;			/* total RAM available */
	uint8_t *out_base;			/* malloc from here */
	int out_count;				/* number of active mallocs */
	int out_size;				/* high-water mark */

	/* We have a limited number of active mallocs. We never free, but we
	 * do reuse slots. */
#define MAX_SLOTS 8
	struct {
		int in_use;			/* is this slot active? */
		void *ptr;			/* starts here */
		size_t size;			/* how big */
	} slots[MAX_SLOTS];
} *bucket;


void *VbExMalloc(size_t size)
{
	int i, j;
	void *ptr = 0;

	if (!bucket) {
		i = shared_mem_size();
		if (EC_SUCCESS != shared_mem_acquire(i, 1, (char **)&bucket)) {
			CPRINTF("FAILED at %s:%d\n", __FILE__, __LINE__);
			ASSERT(0);
		}
		Memset(bucket, 0, sizeof(*bucket));
		bucket->bucket_size = i;
		bucket->out_base = (uint8_t *)(bucket + 1);
		bucket->out_size = sizeof(*bucket);
		DPRINTF("grab the bucket: at 0x%x, size 0x%x\n",
			bucket, bucket->bucket_size);
	}

	if (size % 8) {
		int tmp = (size + 8) & ~0x7ULL;
		DPRINTF("  %d -> %d\n", size, tmp);
		size = tmp;
	}

	for (i = 0; i < MAX_SLOTS; i++) {
		if (!bucket->slots[i].in_use) {

			/* Found an empty one, but reuse the same size if one
			 * already exists. */
			for (j = i; j < MAX_SLOTS; j++) {
				if (!bucket->slots[j].in_use &&
				    size == bucket->slots[j].size) {
					/* empty AND same size */
					bucket->slots[j].in_use = 1;
					ptr = bucket->slots[j].ptr;
					DPRINTF("  = %d (%d)\n", j, size);
					goto out;
				}
			}

			/* no exact matches, must allocate a new chunk */
			ptr = bucket->out_base + bucket->out_size;
			bucket->out_size += size;

			bucket->slots[i].in_use = 1;
			bucket->slots[i].ptr = ptr;
			bucket->slots[i].size = size;
			DPRINTF("  + %d (%d)\n", i, size);
			goto out;
		}
	}

	CPRINTF("FAILED: no empty slots (%d/%d)\n", i, MAX_SLOTS);
	ASSERT(0);

out:
	bucket->out_count++;
	if (bucket->out_size >= bucket->bucket_size) {
		CPRINTF("FAILED: out of memory (%d/%d)\n",
			bucket->out_size, bucket->bucket_size);
		ASSERT(0);
	}

	return ptr;
}


void VbExFree(void *ptr)
{
	int i;

	for (i = 0; i < MAX_SLOTS; i++) {
		if (ptr == bucket->slots[i].ptr) {
			bucket->slots[i].in_use = 0;
			DPRINTF("  - %d (%d)\n", i, bucket->slots[i].size);
			break;
		}
	}
	if (MAX_SLOTS == i) {
		CPRINTF("FAILED: can't find ptr %x!\n", ptr);
		ASSERT(0);
	}

	bucket->out_count--;
	if (!bucket->out_count) {
		DPRINTF("dump the bucket: max used = %d\n", bucket->out_size);
		shared_mem_release(bucket);
		bucket = 0;
	}
}