Root/ubb-vga/physmem.c

1/*
2 * physmem.c - Physical memory allocator
3 *
4 * Written 2011 by Werner Almesberger
5 * Copyright 2011 Werner Almesberger
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 */
12
13
14#include <stdint.h>
15#include <stdlib.h>
16#include <stdio.h>
17#include <unistd.h>
18#include <string.h>
19#include <fcntl.h>
20#include <sys/types.h>
21
22
23#define PAGEMAP_FILE "/proc/self/pagemap"
24#define PAGE_SIZE 4096
25
26#define PM_PSHIFT 55 /* page size */
27#define PM_PSHIFT_MASK 63
28#define PM_SWAPPED 62
29#define PM_PRESENT 63
30
31#define ALIGN 32 /* DMA transfer size */
32
33
34static void align_brk(int alignment)
35{
36    unsigned long addr, remainder;
37
38    addr = (unsigned long) sbrk(0);
39    remainder = addr % alignment;
40    if (remainder)
41        sbrk(alignment-remainder);
42}
43
44
45void **calloc_phys_vec(size_t n, size_t size)
46{
47    void **vec;
48    unsigned long pos;
49    int i;
50
51    vec = malloc(sizeof(void *)*n);
52    if (!vec) {
53        perror("malloc");
54        exit(1);
55    }
56
57    for (i = 0; i != n; i++) {
58        align_brk(ALIGN);
59        pos = (unsigned long) sbrk(0);
60        if ((pos & ~(PAGE_SIZE-1)) != ((pos+size) & ~(PAGE_SIZE-1)))
61            sbrk(PAGE_SIZE-(pos & (PAGE_SIZE-1)));
62        vec[i] = sbrk(size);
63        memset(vec[i], 0, size);
64    }
65    return vec;
66}
67
68
69static unsigned long xlat_one(int fd, unsigned long vaddr)
70{
71    unsigned long page, offset, paddr;
72    ssize_t got;
73    uint64_t map;
74    int pshift;
75
76    offset = vaddr & (PAGE_SIZE-1);
77    page = vaddr/PAGE_SIZE;
78    if (lseek(fd, page*8, SEEK_SET) < 0) {
79        perror("lseek");
80        exit(1);
81    }
82    got = read(fd, &map, 8);
83    if (got < 0) {
84        perror("read");
85        exit(1);
86    }
87    if (got != 8) {
88        fprintf(stderr, "bad read: got %d instead of 8\n", got);
89        exit(1);
90    }
91    if (!((map >> PM_PRESENT) & 1)) {
92        fprintf(stderr, "page %lu is not present\n", page);
93        abort();
94    }
95    if ((map >> PM_SWAPPED) & 1) {
96        fprintf(stderr, "page %lu is swapped\n", page);
97        abort();
98    }
99    pshift = (map >> PM_PSHIFT) & PM_PSHIFT_MASK;
100    if ((1 << pshift) != PAGE_SIZE) {
101        fprintf(stderr, "page claims to have size %u\n", 1 << pshift);
102        abort();
103    }
104    paddr = ((map & 0x7fffffffffffffULL) << pshift) | offset;
105// fprintf(stderr, "0x%lx -> 0x%lx\n", vaddr, paddr);
106    return paddr;
107}
108
109
110unsigned long *xlat_virt(void *const *v, size_t n)
111{
112    unsigned long *res;
113    int fd;
114    int i;
115
116    res = malloc(n*sizeof(unsigned long));
117    if (!res) {
118        perror("malloc");
119        exit(1);
120    }
121    fd = open(PAGEMAP_FILE, O_RDONLY);
122    if (fd < 0) {
123        perror(PAGEMAP_FILE);
124        exit(1);
125    }
126    for (i = 0; i != n; i++)
127        res[i] = xlat_one(fd, (unsigned long) v[i]);
128    close(fd);
129    return res;
130}
131

Archive Download this file

Branches:
master



interactive