ubb-vga2: non-contiguous allocation of frame buffer memory
- ubb-vga.h (calloc_phys_vec), physmem.c: non-contiguous memory allocator
(for now, without really considering any mapping to physical memory)
- Makefile (OBJS): added physmem.o
- ubb-vga.h (grabfb), grabfb.c (grabfb): API change for non-contiguous
frame buffer
- ubb-vga.h (ppmimg), ppmimg.c (convert, ppmimg): API change for
non-contiguous frame buffer
- ubb-vga.h (tstimg), tstimg.c (pixel, color_bars, grill, grid, sides,
dot, line45, arc, printc, text, ctext, tstimg): API change for
non-contiguous frame buffer
- tstimg.c (tstimg): we no longer need to clear the frame buffer
- ubb-vga.c (frame, session, main): use a vector of line buffers instead
of a contiguous frame buffer
2011-04-29 19:04:02 +03:00
|
|
|
/*
|
|
|
|
* physmem.c - Physical memory allocator
|
|
|
|
*
|
|
|
|
* Written 2011 by Werner Almesberger
|
|
|
|
* Copyright 2011 Werner Almesberger
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2011-04-30 03:15:27 +03:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
ubb-vga2: non-contiguous allocation of frame buffer memory
- ubb-vga.h (calloc_phys_vec), physmem.c: non-contiguous memory allocator
(for now, without really considering any mapping to physical memory)
- Makefile (OBJS): added physmem.o
- ubb-vga.h (grabfb), grabfb.c (grabfb): API change for non-contiguous
frame buffer
- ubb-vga.h (ppmimg), ppmimg.c (convert, ppmimg): API change for
non-contiguous frame buffer
- ubb-vga.h (tstimg), tstimg.c (pixel, color_bars, grill, grid, sides,
dot, line45, arc, printc, text, ctext, tstimg): API change for
non-contiguous frame buffer
- tstimg.c (tstimg): we no longer need to clear the frame buffer
- ubb-vga.c (frame, session, main): use a vector of line buffers instead
of a contiguous frame buffer
2011-04-29 19:04:02 +03:00
|
|
|
#include <unistd.h>
|
2011-04-30 03:15:27 +03:00
|
|
|
#include <string.h>
|
|
|
|
#include <fcntl.h>
|
ubb-vga2: non-contiguous allocation of frame buffer memory
- ubb-vga.h (calloc_phys_vec), physmem.c: non-contiguous memory allocator
(for now, without really considering any mapping to physical memory)
- Makefile (OBJS): added physmem.o
- ubb-vga.h (grabfb), grabfb.c (grabfb): API change for non-contiguous
frame buffer
- ubb-vga.h (ppmimg), ppmimg.c (convert, ppmimg): API change for
non-contiguous frame buffer
- ubb-vga.h (tstimg), tstimg.c (pixel, color_bars, grill, grid, sides,
dot, line45, arc, printc, text, ctext, tstimg): API change for
non-contiguous frame buffer
- tstimg.c (tstimg): we no longer need to clear the frame buffer
- ubb-vga.c (frame, session, main): use a vector of line buffers instead
of a contiguous frame buffer
2011-04-29 19:04:02 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
|
2011-04-30 03:15:27 +03:00
|
|
|
#define PAGEMAP_FILE "/proc/self/pagemap"
|
|
|
|
#define PAGE_SIZE 4096
|
|
|
|
|
|
|
|
#define PM_PSHIFT 55 /* page size */
|
|
|
|
#define PM_PSHIFT_MASK 63
|
|
|
|
#define PM_SWAPPED 62
|
|
|
|
#define PM_PRESENT 63
|
ubb-vga2: non-contiguous allocation of frame buffer memory
- ubb-vga.h (calloc_phys_vec), physmem.c: non-contiguous memory allocator
(for now, without really considering any mapping to physical memory)
- Makefile (OBJS): added physmem.o
- ubb-vga.h (grabfb), grabfb.c (grabfb): API change for non-contiguous
frame buffer
- ubb-vga.h (ppmimg), ppmimg.c (convert, ppmimg): API change for
non-contiguous frame buffer
- ubb-vga.h (tstimg), tstimg.c (pixel, color_bars, grill, grid, sides,
dot, line45, arc, printc, text, ctext, tstimg): API change for
non-contiguous frame buffer
- tstimg.c (tstimg): we no longer need to clear the frame buffer
- ubb-vga.c (frame, session, main): use a vector of line buffers instead
of a contiguous frame buffer
2011-04-29 19:04:02 +03:00
|
|
|
|
2011-05-02 10:43:18 +03:00
|
|
|
#define ALIGN 32 /* DMA transfer size */
|
|
|
|
|
ubb-vga2: non-contiguous allocation of frame buffer memory
- ubb-vga.h (calloc_phys_vec), physmem.c: non-contiguous memory allocator
(for now, without really considering any mapping to physical memory)
- Makefile (OBJS): added physmem.o
- ubb-vga.h (grabfb), grabfb.c (grabfb): API change for non-contiguous
frame buffer
- ubb-vga.h (ppmimg), ppmimg.c (convert, ppmimg): API change for
non-contiguous frame buffer
- ubb-vga.h (tstimg), tstimg.c (pixel, color_bars, grill, grid, sides,
dot, line45, arc, printc, text, ctext, tstimg): API change for
non-contiguous frame buffer
- tstimg.c (tstimg): we no longer need to clear the frame buffer
- ubb-vga.c (frame, session, main): use a vector of line buffers instead
of a contiguous frame buffer
2011-04-29 19:04:02 +03:00
|
|
|
|
2011-04-29 19:58:02 +03:00
|
|
|
static void align_brk(int alignment)
|
|
|
|
{
|
2011-05-02 10:43:18 +03:00
|
|
|
unsigned long addr, remainder;
|
2011-04-29 19:58:02 +03:00
|
|
|
|
|
|
|
addr = (unsigned long) sbrk(0);
|
2011-05-02 10:43:18 +03:00
|
|
|
remainder = addr % alignment;
|
|
|
|
if (remainder)
|
|
|
|
sbrk(alignment-remainder);
|
2011-04-29 19:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
ubb-vga2: non-contiguous allocation of frame buffer memory
- ubb-vga.h (calloc_phys_vec), physmem.c: non-contiguous memory allocator
(for now, without really considering any mapping to physical memory)
- Makefile (OBJS): added physmem.o
- ubb-vga.h (grabfb), grabfb.c (grabfb): API change for non-contiguous
frame buffer
- ubb-vga.h (ppmimg), ppmimg.c (convert, ppmimg): API change for
non-contiguous frame buffer
- ubb-vga.h (tstimg), tstimg.c (pixel, color_bars, grill, grid, sides,
dot, line45, arc, printc, text, ctext, tstimg): API change for
non-contiguous frame buffer
- tstimg.c (tstimg): we no longer need to clear the frame buffer
- ubb-vga.c (frame, session, main): use a vector of line buffers instead
of a contiguous frame buffer
2011-04-29 19:04:02 +03:00
|
|
|
void **calloc_phys_vec(size_t n, size_t size)
|
|
|
|
{
|
|
|
|
void **vec;
|
2011-05-02 10:43:18 +03:00
|
|
|
unsigned long pos;
|
ubb-vga2: non-contiguous allocation of frame buffer memory
- ubb-vga.h (calloc_phys_vec), physmem.c: non-contiguous memory allocator
(for now, without really considering any mapping to physical memory)
- Makefile (OBJS): added physmem.o
- ubb-vga.h (grabfb), grabfb.c (grabfb): API change for non-contiguous
frame buffer
- ubb-vga.h (ppmimg), ppmimg.c (convert, ppmimg): API change for
non-contiguous frame buffer
- ubb-vga.h (tstimg), tstimg.c (pixel, color_bars, grill, grid, sides,
dot, line45, arc, printc, text, ctext, tstimg): API change for
non-contiguous frame buffer
- tstimg.c (tstimg): we no longer need to clear the frame buffer
- ubb-vga.c (frame, session, main): use a vector of line buffers instead
of a contiguous frame buffer
2011-04-29 19:04:02 +03:00
|
|
|
int i;
|
|
|
|
|
2011-04-30 03:15:27 +03:00
|
|
|
vec = malloc(sizeof(void *)*n);
|
|
|
|
if (!vec) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
2011-04-29 19:58:02 +03:00
|
|
|
|
ubb-vga2: non-contiguous allocation of frame buffer memory
- ubb-vga.h (calloc_phys_vec), physmem.c: non-contiguous memory allocator
(for now, without really considering any mapping to physical memory)
- Makefile (OBJS): added physmem.o
- ubb-vga.h (grabfb), grabfb.c (grabfb): API change for non-contiguous
frame buffer
- ubb-vga.h (ppmimg), ppmimg.c (convert, ppmimg): API change for
non-contiguous frame buffer
- ubb-vga.h (tstimg), tstimg.c (pixel, color_bars, grill, grid, sides,
dot, line45, arc, printc, text, ctext, tstimg): API change for
non-contiguous frame buffer
- tstimg.c (tstimg): we no longer need to clear the frame buffer
- ubb-vga.c (frame, session, main): use a vector of line buffers instead
of a contiguous frame buffer
2011-04-29 19:04:02 +03:00
|
|
|
for (i = 0; i != n; i++) {
|
2011-05-02 10:43:18 +03:00
|
|
|
align_brk(ALIGN);
|
|
|
|
pos = (unsigned long) sbrk(0);
|
|
|
|
if ((pos & ~(PAGE_SIZE-1)) != ((pos+size) & ~(PAGE_SIZE-1)))
|
|
|
|
sbrk(PAGE_SIZE-(pos & (PAGE_SIZE-1)));
|
ubb-vga2: non-contiguous allocation of frame buffer memory
- ubb-vga.h (calloc_phys_vec), physmem.c: non-contiguous memory allocator
(for now, without really considering any mapping to physical memory)
- Makefile (OBJS): added physmem.o
- ubb-vga.h (grabfb), grabfb.c (grabfb): API change for non-contiguous
frame buffer
- ubb-vga.h (ppmimg), ppmimg.c (convert, ppmimg): API change for
non-contiguous frame buffer
- ubb-vga.h (tstimg), tstimg.c (pixel, color_bars, grill, grid, sides,
dot, line45, arc, printc, text, ctext, tstimg): API change for
non-contiguous frame buffer
- tstimg.c (tstimg): we no longer need to clear the frame buffer
- ubb-vga.c (frame, session, main): use a vector of line buffers instead
of a contiguous frame buffer
2011-04-29 19:04:02 +03:00
|
|
|
vec[i] = sbrk(size);
|
|
|
|
memset(vec[i], 0, size);
|
|
|
|
}
|
|
|
|
return vec;
|
|
|
|
}
|
2011-04-30 03:15:27 +03:00
|
|
|
|
|
|
|
|
|
|
|
static unsigned long xlat_one(int fd, unsigned long vaddr)
|
|
|
|
{
|
|
|
|
unsigned long page, offset, paddr;
|
|
|
|
ssize_t got;
|
|
|
|
uint64_t map;
|
|
|
|
int pshift;
|
|
|
|
|
|
|
|
offset = vaddr & (PAGE_SIZE-1);
|
|
|
|
page = vaddr/PAGE_SIZE;
|
|
|
|
if (lseek(fd, page*8, SEEK_SET) < 0) {
|
|
|
|
perror("lseek");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
got = read(fd, &map, 8);
|
|
|
|
if (got < 0) {
|
|
|
|
perror("read");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (got != 8) {
|
|
|
|
fprintf(stderr, "bad read: got %d instead of 8\n", got);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
if (!((map >> PM_PRESENT) & 1)) {
|
|
|
|
fprintf(stderr, "page %lu is not present\n", page);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
if ((map >> PM_SWAPPED) & 1) {
|
|
|
|
fprintf(stderr, "page %lu is swapped\n", page);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
pshift = (map >> PM_PSHIFT) & PM_PSHIFT_MASK;
|
|
|
|
if ((1 << pshift) != PAGE_SIZE) {
|
|
|
|
fprintf(stderr, "page claims to have size %u\n", 1 << pshift);
|
|
|
|
abort();
|
|
|
|
}
|
|
|
|
paddr = ((map & 0x7fffffffffffffULL) << pshift) | offset;
|
2011-05-02 10:43:18 +03:00
|
|
|
// fprintf(stderr, "0x%lx -> 0x%lx\n", vaddr, paddr);
|
2011-04-30 03:15:27 +03:00
|
|
|
return paddr;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
unsigned long *xlat_virt(void *const *v, size_t n)
|
|
|
|
{
|
|
|
|
unsigned long *res;
|
|
|
|
int fd;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
res = malloc(n*sizeof(unsigned long));
|
|
|
|
if (!res) {
|
|
|
|
perror("malloc");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
fd = open(PAGEMAP_FILE, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
perror(PAGEMAP_FILE);
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
for (i = 0; i != n; i++)
|
|
|
|
res[i] = xlat_one(fd, (unsigned long) v[i]);
|
|
|
|
close(fd);
|
|
|
|
return res;
|
|
|
|
}
|