OpenCores
URL https://opencores.org/ocsvn/c0or1k/c0or1k/trunk

Subversion Repositories c0or1k

[/] [c0or1k/] [trunk/] [conts/] [test_suite0/] [src/] [api/] [memory.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 drasko
/*
2
 * Empty virtual and physical pages for
3
 * creating test scenarios
4
 *
5
 * Copyright (C) 2010 B Labs Ltd.
6
 *
7
 * Author: Bahadir Balban
8
 */
9
#include <l4lib/lib/addr.h>
10
#include INC_GLUE(memory.h)
11
#include <l4/generic/cap-types.h>
12
#include <l4lib/lib/cap.h>
13
#include <l4/lib/math.h>
14
#include <stdio.h>
15
#include <memory.h>
16
#include <linker.h>
17
#include <tests.h>
18
 
19
/*
20
 * Declare a statically allocated char buffer
21
 * with enough bitmap size to cover given size
22
 */
23
#define DECLARE_IDPOOL(name, size)      \
24
        char name[(sizeof(struct id_pool) + ((size >> 12) >> 3))]
25
 
26
struct address_pool virtual_page_pool, physical_page_pool;
27
 
28
#define PAGE_POOL_SIZE                  SZ_16MB
29
 
30
DECLARE_IDPOOL(virtual_idpool, PAGE_POOL_SIZE);
31
DECLARE_IDPOOL(physical_idpool, PAGE_POOL_SIZE);
32
 
33
#define virt_to_phys(virtual)   ((unsigned long)(virtual) - (unsigned long)(offset))
34
#define phys_to_virt(physical)  ((unsigned long)(physical) + (unsigned long)(offset))
35
 
36
 
37
#define TEST_POOL_TOTAL         5
38
/*
39
 * Test page pool
40
 */
41
void test_page_pool(void)
42
{
43
        void *p[TEST_POOL_TOTAL], *v[TEST_POOL_TOTAL];
44
 
45
        /* Allocate test pages */
46
        for (int i = 0; i < TEST_POOL_TOTAL; i++) {
47
                v[i] = virtual_page_new(1);
48
                p[i] = physical_page_new(1);
49
                dbg_printf("Test allocated: Virtual%d: 0x%p, "
50
                           "Physical%d, 0x%p\n",
51
                           i, v[i], i, p[i]);
52
        }
53
 
54
        /* Free test pages */
55
        for (int i = 0; i < TEST_POOL_TOTAL; i++) {
56
                virtual_page_free(v[i], 1);
57
                physical_page_free(p[i], 1);
58
        }
59
 
60
        /* Re-allocate test pages */
61
        for (int i = 0; i < TEST_POOL_TOTAL; i++) {
62
                v[i] = virtual_page_new(1);
63
                p[i] = physical_page_new(1);
64
                dbg_printf("Test allocated: Virtual%d: 0x%p, "
65
                           "Physical%d, 0x%p\n",
66
                           i, v[i], i, p[i]);
67
        }
68
 
69
        /* Free test pages */
70
        for (int i = 0; i < TEST_POOL_TOTAL; i++) {
71
                virtual_page_free(v[i], 1);
72
                physical_page_free(p[i], 1);
73
        }
74
 
75
        /* Allocate in different lengths */
76
        for (int i = 0; i < TEST_POOL_TOTAL; i++) {
77
                v[i] = virtual_page_new(i);
78
                p[i] = physical_page_new(i);
79
                dbg_printf("Test allocated: Virtual%d: 0x%p, "
80
                           "Physical%d, 0x%p\n",
81
                           i, v[i], i, p[i]);
82
        }
83
 
84
        /* Free test pages in different order */
85
        for (int i = TEST_POOL_TOTAL - 1; i >= 0; i--) {
86
                virtual_page_free(v[i], 1);
87
                physical_page_free(p[i], 1);
88
        }
89
 
90
        /* Allocate in different lengths */
91
        for (int i = 0; i < TEST_POOL_TOTAL; i++) {
92
                v[i] = virtual_page_new(i);
93
                p[i] = physical_page_new(i);
94
                dbg_printf("Test allocated: Virtual%d: 0x%p, "
95
                           "Physical%d, 0x%p\n",
96
                           i, v[i], i, p[i]);
97
        }
98
 
99
        /* Free test pages in normal order */
100
        for (int i = 0; i < TEST_POOL_TOTAL; i++) {
101
                virtual_page_free(v[i], 1);
102
                physical_page_free(p[i], 1);
103
        }
104
}
105
 
106
void page_pool_init(void)
107
{
108
        struct capability *physcap, *virtcap;
109
        unsigned long phys_start, phys_end;
110
        unsigned long virt_start, virt_end;
111
 
112
        /*
113
         * Get physmem capability (Must be only one)
114
         */
115
        if (!(physcap = cap_get_physmem(CAP_TYPE_MAP_PHYSMEM))) {
116
                printf("FATAL: Could not find a physical memory"
117
                       "capability to use as a page pool.\n");
118
                BUG();
119
        }
120
 
121
        /*
122
         * Get virtmem capability (Must be only one)
123
         */
124
        if (!(virtcap = cap_get_by_type(CAP_TYPE_MAP_VIRTMEM))) {
125
                printf("FATAL: Could not find a virtual memory"
126
                       "capability to use as a page pool.\n");
127
                BUG();
128
        }
129
 
130
        /*
131
         * Now initialize physical and virtual page marks
132
         * from unused pages. Linker script will help us
133
         * on this.
134
         */
135
        /*
136
        printf("__data_start symbol: %lx\n", (unsigned long)__data_start);
137
        printf("__data_end symbol: %lx\n", (unsigned long)__data_end);
138
        printf("__bss_start symbol: %lx\n", (unsigned long)__bss_start);
139
        printf("__bss_end symbol: %lx\n", (unsigned long)__bss_end);
140
        printf("__stack_start symbol: %lx\n", (unsigned long)__stack_start);
141
        printf("__stack_end symbol: %lx\n", (unsigned long)__stack_end);
142
        printf("__end symbol: %lx\n", (unsigned long)__end);
143
        */
144
 
145
        phys_start = page_align_up(virt_to_phys(__end) +
146
                                   (unsigned long)lma_start);
147
        phys_end = __pfn_to_addr(physcap->end);
148
 
149
        dbg_printf("%s: Initializing physical range 0x%lx - 0x%lx\n",
150
                   __FUNCTION__, phys_start, phys_end);
151
 
152
        virt_start = page_align_up(__end) + (unsigned long)lma_start;
153
        virt_end = __pfn_to_addr(virtcap->end);
154
 
155
        dbg_printf("%s: Initializing virtual range 0x%lx - 0x%lx\n",
156
                   __FUNCTION__, virt_start, virt_end);
157
 
158
        /* Initialize pools, maximum of PAGE_POOL_SIZE size */
159
        address_pool_init(&virtual_page_pool,
160
                          (struct id_pool *)&virtual_idpool,
161
                          virt_start, min(virt_end,
162
                                          virt_start + PAGE_POOL_SIZE));
163
        address_pool_init(&physical_page_pool,
164
                          (struct id_pool *)&physical_idpool,
165
                          phys_start, min(phys_end,
166
                                          phys_start + PAGE_POOL_SIZE));
167
 
168
        // test_page_pool();
169
}
170
 
171
/*
172
 * Some tests require page-faulting virtual addresses or
173
 * differing virtual addresses that map onto the same
174
 * physical page. These functions provide these pages.
175
 */
176
 
177
void *virtual_page_new(int npages)
178
{
179
        return address_new(&virtual_page_pool, npages, PAGE_SIZE);
180
}
181
 
182
void *physical_page_new(int npages)
183
{
184
        return address_new(&physical_page_pool, npages, PAGE_SIZE);
185
}
186
 
187
void virtual_page_free(void *address, int npages)
188
{
189
        address_del(&virtual_page_pool, address,
190
                    npages, PAGE_SIZE);
191
}
192
 
193
void physical_page_free(void *address, int npages)
194
{
195
        address_del(&physical_page_pool, address,
196
                    npages, PAGE_SIZE);
197
}
198
 

powered by: WebSVN 2.1.0

© copyright 1999-2025 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.