mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
This commit is a large squash, the full development branch is available at: https://github.com/cirosantilli/linux-kernel-module-cheat/tree/asm This notably means a refactor of the userland build and testing, to support: - improved assembly infrastructure unified across arm and x86 - native in-tree build and test helpers - parallel building and testing, which implies thread_pool.py - selection of what to build and test from the CLI - path_properties.py to indicate how to build and run different examples - in full system, move all userland stuff into /lkmc - prefix everything that we defined across files with LKMC - --gdb uber convenient helper - remove import imp which was deprecated Full commit messages from the branch follow: 1: userland: add assembly support Move arm assembly cheat here, and start some work on x86 cheat as well. 2: document userland asm syscall interfaces 3: userland assembly: structure readme 4: x86 fail works 5: asm: more links 6: userland: add ported to all archs 7: move all our stuff into /lkmc in guest Motivation: userland is getting several new subdirectories, it would be too insane to just dump all of that in the guest root filesystem. To alleviate the cd pain, .profile puts user inside /lkmc by default. 8: start the big userland migration 9: migrate all 10: bak 11: build-userland-in-tree is now a Python command ./build calls it, we did this to allow --download-dependencies to work perfectly. 12: rename include to lkmc 13: mtops.h is perfect now 14: userland: make build perfect 15: preparing test_user_mode, need to generalize stuff as usual 16: asm: prefix every linux specific with linux/ 17: userland: maybe it really works 18: userland: fix kernel version to work on older ubuntu Expose --kernel-version to allow customization. Update LTP info. 19: userland: build really truly working now userland test: start work, in a working state, but no features 20: test-user-mode: make perfect like build-userland Multithreading and target selection. 21: userland: get a bit closer to perfection 22: thread_pool: support passing thread IDs Then use that to fix gem5 error log read race. 23: userland: native testing 24: userland: path properties getting nice! 25: userland: move posix/environ from cpp-cheat 26: gem5: --debug-flags without =, looks nicer whenever it can be done 27: run: rename --wait-gdb in --gdb-wait, --gdb prefix might become a thing 28: run: create --tmux-program gdb to open gem5 GDB 29: run: create the uber convenient --gdb option 30: userland: move getchar from cpp-cheat 31: prebuilt: kernel boot aarch64 does not work on Ubuntu 16.04 32: userland: x86_64 linux hello world make PIE 33: userland: try to make userland executable selection saner Only allow existing files to be built, stop extension expansion madness. cli_function: get_cli print booleans properly, was printing without --no- for negations. 34: userland: only link to lkmc.o if needed 35: path_properties: make data very compact with only tuples and dicts Spend 2 hours of my life thinking about low value tree walks ;-) 36: userland: move more userland/arch/ logic into property tree 37: userland: make libs work Working for build, but now test-user-mode-in-tree is not using --in-tree, TODO fix later on. 38: userland: make libs really work 39: userland: document path_properties 40: userland: classify linux 41: waste your life 42: common: fix absolute path runs --gdb: allow running from arbitrary directory 43: baremetal: arm allow using floating point instructions 44: baremetal: stat preparing to make perfect like userland/ 45: run: fix image check logic accounting for userland Was failing if I try to run userland (with abspath) when out/ directory is not present. 46: cli-function: raise if the config file is given and does not exist 47: common: define missing 'ld' variable, this broke m5 build 48: rum: --qemu-which host now works for user mode as well as system Don't fall back on host QEMU automatically, too much insanity. 49: userland: refix silly mistakes 50: userland: use path_properties flags for all builds, including lkmc. and userland/arch/main.c Without this in particular, --gdb fails on assembly because main.c was not being built with -ggdb3. 51: userland: start refactor to show failing values on failure! aarch64 basically done, but missing: - other archs - maybe convert main.c into C++ to use templates? - full review of ASSERT_EQ calling convention issues not seen by tests by chance - documentation 52: readme: releases are more stable... 53: submodules: sort gitmodules 54: test-baremetal: same interface as test-user-mode In particular, runs tests in parallel, and allows selecting given tests 55: baremetal: allow arbitrary exit status with the magic string test-baremetal: fix missing setting x0 return value Examples were just returning on ret without setting x0, which led to failures... those were not noticed because of how broken the testing system was ;-) 56: baremetal: ah, actually nope, it didn't work :-( Workaround for now. Works on asserts, but not on exit 1. Some other day, maybe. https://github.com/cirosantilli/linux-kernel-module-cheat/issues/59 57: panic on panic: improve behaviour description 58: baremetal: get exit status working with on_exit :-) 59: baremetal: implement C assert 60: test-baremetal: remove commented out exit status workaround 61: test-user-mode: handle exit status for signals. Fix #61. 62: aarch64: fix ASSERT_EQ_REG tests on gem5 Was doing an 8-byte aligned store, which gem5 dislikes. But the ARMARM says bad things may happen there, notably a signal: "D1.8.2 SP alignment checking" so gem5 is not really too wrong, QEMU just happens to work by chance. 63: userland assembly: build empty.S and fail.S to toplevel and run fail.S with path_properties exit_status They were just duplicating stuff needlessly while we don't support non-native in-tree builds, which leads to executable conflicts for C file anyways. 64: gem5: use a single build tree for all build types gem5 already has different object names for each build type it seems, so let's just make sure that works and save some disk space. 65: userland x86_64: ASSERT_EQ show actual and expected values 66: assert_fail.c: add to readme index 67: userland x86_64: implement ASSERT_MEMCMP 68: userland x86_64: allow ASSERT_EQ to take just about anything 69: gas data sizes 70: gas_data_sizes.S: make PIE for all ISAs 71: x86: paddq 72: x86 paddq: test entire family 73: Get rid of imp, started giving deprecation warning every time in Python 3.7 in Ubuntu 19.04. Please python stop torturing me with refactors. Make ./run -u blow up if executable not found, otherwise I go crazy. Get ./test-gdb back to life after the ./run relative path refactor, forgot to test this. 74: fix run-toolchain, qemu-monitor, trace-boot, trace2line, bisect-linux-boot-gem5. Fixes part of #63 I'm sad no one reported qemu-monitor break, that one is kind of important. count.out arguments broke it as an init program, since the kernel adds trash parameters to every init. Is anyone using this repo, I wonder? Keep pushing, keep pushing. One day it gets good enough, and the whole world will see. 75: x86 assembly: addpd 76: Fix import_path circular dependency by splitting it out. Use import thread_pool instead from, from is evil. Fix poweroff.out path for ./trace-boot. 77: run: rename cryptic tmu to tmux-split, ./run is good now so I never use it anymore explicitly 78: assembly SIMD add: make uniform for all ISAs, mark as entry point to learning SIMD 79: start moving arm-assembly-cheat readme in here 80: arm assembly: move some more in 81: move more arm in 82: userland: attempt to fix all assembly example links to README 83: assembly: improve organization of simd add 84: ld2 move in 85: Make userland / assembly getting started more uniform / visible Forward --gcc-which to ./run --tmux. Use gdb-multiarch for --gcc-which host. 86: userland: disable PIE explicitly on command line for all executables 87: userland: make userland content a better landing page 88: build: check git version from --version and degrade gracefully 89: build: make --dry-run work again on all 90: import_path: importlib explicit for Ubuntu 16.04 91: make all submodules point to my forks git servers are insane, submodule implementation is crap, what can you do 92: build: log warning on git too old for --update 93: build-linux: do olddefconfig even if no fragments In particular, gem5 kernel 4.15 needs it 94: userland content: improve a bit landing page for cpp-cheat
741 lines
26 KiB
C
741 lines
26 KiB
C
/* https://github.com/cirosantilli/linux-kernel-module-cheat#drm
|
|
* Adapted from: https://github.com/dvdhrm/docs/blob/fad7c3203b14e67053e0fc41d8490138b8ff47dd/drm-howto/modeset.c */
|
|
|
|
/*
|
|
* modeset - DRM Modesetting Example
|
|
*
|
|
* Written 2012 by David Herrmann <dh.herrmann@googlemail.com>
|
|
* Dedicated to the Public Domain.
|
|
*/
|
|
|
|
/*
|
|
* DRM Modesetting Howto
|
|
* This document describes the DRM modesetting API. Before we can use the DRM
|
|
* API, we have to include xf86drm.h and xf86drmMode.h. Both are provided by
|
|
* libdrm which every major distribution ships by default. It has no other
|
|
* dependencies and is pretty small.
|
|
*
|
|
* Please ignore all forward-declarations of functions which are used later. I
|
|
* reordered the functions so you can read this document from top to bottom. If
|
|
* you reimplement it, you would probably reorder the functions to avoid all the
|
|
* nasty forward declarations.
|
|
*
|
|
* For easier reading, we ignore all memory-allocation errors of malloc() and
|
|
* friends here. However, we try to correctly handle all other kinds of errors
|
|
* that may occur.
|
|
*
|
|
* All functions and global variables are prefixed with "modeset_*" in this
|
|
* file. So it should be clear whether a function is a local helper or if it is
|
|
* provided by some external library.
|
|
*/
|
|
|
|
#define _GNU_SOURCE
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <sys/mman.h>
|
|
#include <time.h>
|
|
#include <unistd.h>
|
|
#include <xf86drm.h>
|
|
#include <xf86drmMode.h>
|
|
|
|
struct modeset_dev;
|
|
static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
|
|
struct modeset_dev *dev);
|
|
static int modeset_create_fb(int fd, struct modeset_dev *dev);
|
|
static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
|
|
struct modeset_dev *dev);
|
|
static int modeset_open(int *out, const char *node);
|
|
static int modeset_prepare(int fd);
|
|
static void modeset_draw(void);
|
|
static void modeset_cleanup(int fd);
|
|
|
|
/*
|
|
* When the linux kernel detects a graphics-card on your machine, it loads the
|
|
* correct device driver (located in kernel-tree at ./drivers/gpu/drm/<xy>) and
|
|
* provides two character-devices to control it. Udev (or whatever hotplugging
|
|
* application you use) will create them as:
|
|
* /dev/dri/card0
|
|
* /dev/dri/controlID64
|
|
* We only need the first one. You can hard-code this path into your application
|
|
* like we do here, but it is recommended to use libudev with real hotplugging
|
|
* and multi-seat support. However, this is beyond the scope of this document.
|
|
* Also note that if you have multiple graphics-cards, there may also be
|
|
* /dev/dri/card1, /dev/dri/card2, ...
|
|
*
|
|
* We simply use /dev/dri/card0 here but the user can specify another path on
|
|
* the command line.
|
|
*
|
|
* modeset_open(out, node): This small helper function opens the DRM device
|
|
* which is given as @node. The new fd is stored in @out on success. On failure,
|
|
* a negative error code is returned.
|
|
* After opening the file, we also check for the DRM_CAP_DUMB_BUFFER capability.
|
|
* If the driver supports this capability, we can create simple memory-mapped
|
|
* buffers without any driver-dependent code. As we want to avoid any radeon,
|
|
* nvidia, intel, etc. specific code, we depend on DUMB_BUFFERs here.
|
|
*/
|
|
|
|
static int modeset_open(int *out, const char *node)
|
|
{
|
|
int fd, ret;
|
|
uint64_t has_dumb;
|
|
|
|
fd = open(node, O_RDWR | O_CLOEXEC);
|
|
if (fd < 0) {
|
|
ret = -errno;
|
|
fprintf(stderr, "cannot open '%s': %s\n", node, strerror(errno));
|
|
return ret;
|
|
}
|
|
|
|
if (drmGetCap(fd, DRM_CAP_DUMB_BUFFER, &has_dumb) < 0 ||
|
|
!has_dumb) {
|
|
fprintf(stderr, "drm device '%s' does not support dumb buffers\n",
|
|
node);
|
|
close(fd);
|
|
return -EOPNOTSUPP;
|
|
}
|
|
|
|
*out = fd;
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* As a next step we need to find our available display devices. libdrm provides
|
|
* a drmModeRes structure that contains all the needed information. We can
|
|
* retrieve it via drmModeGetResources(fd) and free it via
|
|
* drmModeFreeResources(res) again.
|
|
*
|
|
* A physical connector on your graphics card is called a "connector". You can
|
|
* plug a monitor into it and control what is displayed. We are definitely
|
|
* interested in what connectors are currently used, so we simply iterate
|
|
* through the list of connectors and try to display a test-picture on each
|
|
* available monitor.
|
|
* However, this isn't as easy as it sounds. First, we need to check whether the
|
|
* connector is actually used (a monitor is plugged in and turned on). Then we
|
|
* need to find a CRTC that can control this connector. CRTCs are described
|
|
* later on. After that we create a framebuffer object. If we have all this, we
|
|
* can mmap() the framebuffer and draw a test-picture into it. Then we can tell
|
|
* the DRM device to show the framebuffer on the given CRTC with the selected
|
|
* connector.
|
|
*
|
|
* As we want to draw moving pictures on the framebuffer, we actually have to
|
|
* remember all these settings. Therefore, we create one "struct modeset_dev"
|
|
* object for each connector+crtc+framebuffer pair that we successfully
|
|
* initialized and push it into the global device-list.
|
|
*
|
|
* Each field of this structure is described when it is first used. But as a
|
|
* summary:
|
|
* "struct modeset_dev" contains: {
|
|
* - @next: points to the next device in the single-linked list
|
|
*
|
|
* - @width: width of our buffer object
|
|
* - @height: height of our buffer object
|
|
* - @stride: stride value of our buffer object
|
|
* - @size: size of the memory mapped buffer
|
|
* - @handle: a DRM handle to the buffer object that we can draw into
|
|
* - @map: pointer to the memory mapped buffer
|
|
*
|
|
* - @mode: the display mode that we want to use
|
|
* - @fb: a framebuffer handle with our buffer object as scanout buffer
|
|
* - @conn: the connector ID that we want to use with this buffer
|
|
* - @crtc: the crtc ID that we want to use with this connector
|
|
* - @saved_crtc: the configuration of the crtc before we changed it. We use it
|
|
* so we can restore the same mode when we exit.
|
|
* }
|
|
*/
|
|
|
|
struct modeset_dev {
|
|
struct modeset_dev *next;
|
|
|
|
uint32_t width;
|
|
uint32_t height;
|
|
uint32_t stride;
|
|
uint32_t size;
|
|
uint32_t handle;
|
|
uint8_t *map;
|
|
|
|
drmModeModeInfo mode;
|
|
uint32_t fb;
|
|
uint32_t conn;
|
|
uint32_t crtc;
|
|
drmModeCrtc *saved_crtc;
|
|
};
|
|
|
|
static struct modeset_dev *modeset_list = NULL;
|
|
|
|
/*
|
|
* So as next step we need to actually prepare all connectors that we find. We
|
|
* do this in this little helper function:
|
|
*
|
|
* modeset_prepare(fd): This helper function takes the DRM fd as argument and
|
|
* then simply retrieves the resource-info from the device. It then iterates
|
|
* through all connectors and calls other helper functions to initialize this
|
|
* connector (described later on).
|
|
* If the initialization was successful, we simply add this object as new device
|
|
* into the global modeset device list.
|
|
*
|
|
* The resource-structure contains a list of all connector-IDs. We use the
|
|
* helper function drmModeGetConnector() to retrieve more information on each
|
|
* connector. After we are done with it, we free it again with
|
|
* drmModeFreeConnector().
|
|
* Our helper modeset_setup_dev() returns -ENOENT if the connector is currently
|
|
* unused and no monitor is plugged in. So we can ignore this connector.
|
|
*/
|
|
|
|
static int modeset_prepare(int fd)
|
|
{
|
|
drmModeRes *res;
|
|
drmModeConnector *conn;
|
|
unsigned int i;
|
|
struct modeset_dev *dev;
|
|
int ret;
|
|
|
|
/* retrieve resources */
|
|
res = drmModeGetResources(fd);
|
|
if (!res) {
|
|
fprintf(stderr, "cannot retrieve DRM resources (%d): %s\n",
|
|
errno, strerror(errno));
|
|
return -errno;
|
|
}
|
|
|
|
/* iterate all connectors */
|
|
for (i = 0; i < (unsigned int)res->count_connectors; ++i) {
|
|
/* get information for each connector */
|
|
conn = drmModeGetConnector(fd, res->connectors[i]);
|
|
if (!conn) {
|
|
fprintf(stderr, "cannot retrieve DRM connector %u:%u (%d): %s\n",
|
|
i, res->connectors[i], errno, strerror(errno));
|
|
continue;
|
|
}
|
|
|
|
/* create a device structure */
|
|
dev = malloc(sizeof(*dev));
|
|
memset(dev, 0, sizeof(*dev));
|
|
dev->conn = conn->connector_id;
|
|
|
|
/* call helper function to prepare this connector */
|
|
ret = modeset_setup_dev(fd, res, conn, dev);
|
|
if (ret) {
|
|
if (ret != -ENOENT) {
|
|
errno = -ret;
|
|
fprintf(stderr, "cannot setup device for connector %u:%u (%d): %s\n",
|
|
i, res->connectors[i], errno, strerror(errno));
|
|
}
|
|
free(dev);
|
|
drmModeFreeConnector(conn);
|
|
continue;
|
|
}
|
|
|
|
/* free connector data and link device into global list */
|
|
drmModeFreeConnector(conn);
|
|
dev->next = modeset_list;
|
|
modeset_list = dev;
|
|
}
|
|
|
|
/* free resources again */
|
|
drmModeFreeResources(res);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Now we dig deeper into setting up a single connector. As described earlier,
|
|
* we need to check several things first:
|
|
* * If the connector is currently unused, that is, no monitor is plugged in,
|
|
* then we can ignore it.
|
|
* * We have to find a suitable resolution and refresh-rate. All this is
|
|
* available in drmModeModeInfo structures saved for each crtc. We simply
|
|
* use the first mode that is available. This is always the mode with the
|
|
* highest resolution.
|
|
* A more sophisticated mode-selection should be done in real applications,
|
|
* though.
|
|
* * Then we need to find an CRTC that can drive this connector. A CRTC is an
|
|
* internal resource of each graphics-card. The number of CRTCs controls how
|
|
* many connectors can be controlled indepedently. That is, a graphics-cards
|
|
* may have more connectors than CRTCs, which means, not all monitors can be
|
|
* controlled independently.
|
|
* There is actually the possibility to control multiple connectors via a
|
|
* single CRTC if the monitors should display the same content. However, we
|
|
* do not make use of this here.
|
|
* So think of connectors as pipelines to the connected monitors and the
|
|
* CRTCs are the controllers that manage which data goes to which pipeline.
|
|
* If there are more pipelines than CRTCs, then we cannot control all of
|
|
* them at the same time.
|
|
* * We need to create a framebuffer for this connector. A framebuffer is a
|
|
* memory buffer that we can write XRGB32 data into. So we use this to
|
|
* render our graphics and then the CRTC can scan-out this data from the
|
|
* framebuffer onto the monitor.
|
|
*/
|
|
|
|
static int modeset_setup_dev(int fd, drmModeRes *res, drmModeConnector *conn,
|
|
struct modeset_dev *dev)
|
|
{
|
|
int ret;
|
|
|
|
/* check if a monitor is connected */
|
|
if (conn->connection != DRM_MODE_CONNECTED) {
|
|
fprintf(stderr, "ignoring unused connector %u\n",
|
|
conn->connector_id);
|
|
return -ENOENT;
|
|
}
|
|
|
|
/* check if there is at least one valid mode */
|
|
if (conn->count_modes == 0) {
|
|
fprintf(stderr, "no valid mode for connector %u\n",
|
|
conn->connector_id);
|
|
return -EFAULT;
|
|
}
|
|
|
|
/* copy the mode information into our device structure */
|
|
memcpy(&dev->mode, &conn->modes[0], sizeof(dev->mode));
|
|
dev->width = conn->modes[0].hdisplay;
|
|
dev->height = conn->modes[0].vdisplay;
|
|
fprintf(stderr, "mode for connector %u is %ux%u\n",
|
|
conn->connector_id, dev->width, dev->height);
|
|
|
|
/* find a crtc for this connector */
|
|
ret = modeset_find_crtc(fd, res, conn, dev);
|
|
if (ret) {
|
|
fprintf(stderr, "no valid crtc for connector %u\n",
|
|
conn->connector_id);
|
|
return ret;
|
|
}
|
|
|
|
/* create a framebuffer for this CRTC */
|
|
ret = modeset_create_fb(fd, dev);
|
|
if (ret) {
|
|
fprintf(stderr, "cannot create framebuffer for connector %u\n",
|
|
conn->connector_id);
|
|
return ret;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* modeset_find_crtc(fd, res, conn, dev): This small helper tries to find a
|
|
* suitable CRTC for the given connector. We have actually have to introduce one
|
|
* more DRM object to make this more clear: Encoders.
|
|
* Encoders help the CRTC to convert data from a framebuffer into the right
|
|
* format that can be used for the chosen connector. We do not have to
|
|
* understand any more of these conversions to make use of it. However, you must
|
|
* know that each connector has a limited list of encoders that it can use. And
|
|
* each encoder can only work with a limited list of CRTCs. So what we do is
|
|
* trying each encoder that is available and looking for a CRTC that this
|
|
* encoder can work with. If we find the first working combination, we are happy
|
|
* and write it into the @dev structure.
|
|
* But before iterating all available encoders, we first try the currently
|
|
* active encoder+crtc on a connector to avoid a full modeset.
|
|
*
|
|
* However, before we can use a CRTC we must make sure that no other device,
|
|
* that we setup previously, is already using this CRTC. Remember, we can only
|
|
* drive one connector per CRTC! So we simply iterate through the "modeset_list"
|
|
* of previously setup devices and check that this CRTC wasn't used before.
|
|
* Otherwise, we continue with the next CRTC/Encoder combination.
|
|
*/
|
|
|
|
static int modeset_find_crtc(int fd, drmModeRes *res, drmModeConnector *conn,
|
|
struct modeset_dev *dev)
|
|
{
|
|
drmModeEncoder *enc;
|
|
unsigned int i, j;
|
|
int32_t crtc;
|
|
struct modeset_dev *iter;
|
|
|
|
/* first try the currently conected encoder+crtc */
|
|
if (conn->encoder_id)
|
|
enc = drmModeGetEncoder(fd, conn->encoder_id);
|
|
else
|
|
enc = NULL;
|
|
|
|
if (enc) {
|
|
if (enc->crtc_id) {
|
|
crtc = enc->crtc_id;
|
|
for (iter = modeset_list; iter; iter = iter->next) {
|
|
if ((int32_t)iter->crtc == crtc) {
|
|
crtc = -1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (crtc >= 0) {
|
|
drmModeFreeEncoder(enc);
|
|
dev->crtc = crtc;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
drmModeFreeEncoder(enc);
|
|
}
|
|
|
|
/* If the connector is not currently bound to an encoder or if the
|
|
* encoder+crtc is already used by another connector (actually unlikely
|
|
* but lets be safe), iterate all other available encoders to find a
|
|
* matching CRTC. */
|
|
for (i = 0; i < (unsigned int)conn->count_encoders; ++i) {
|
|
enc = drmModeGetEncoder(fd, conn->encoders[i]);
|
|
if (!enc) {
|
|
fprintf(stderr, "cannot retrieve encoder %u:%u (%d): %s\n",
|
|
i, conn->encoders[i], errno, strerror(errno));
|
|
continue;
|
|
}
|
|
|
|
/* iterate all global CRTCs */
|
|
for (j = 0; j < (unsigned int)res->count_crtcs; ++j) {
|
|
/* check whether this CRTC works with the encoder */
|
|
if (!(enc->possible_crtcs & (1 << j)))
|
|
continue;
|
|
|
|
/* check that no other device already uses this CRTC */
|
|
crtc = res->crtcs[j];
|
|
for (iter = modeset_list; iter; iter = iter->next) {
|
|
if ((int32_t)iter->crtc == crtc) {
|
|
crtc = -1;
|
|
break;
|
|
}
|
|
}
|
|
|
|
/* we have found a CRTC, so save it and return */
|
|
if (crtc >= 0) {
|
|
drmModeFreeEncoder(enc);
|
|
dev->crtc = crtc;
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
drmModeFreeEncoder(enc);
|
|
}
|
|
|
|
fprintf(stderr, "cannot find suitable CRTC for connector %u\n",
|
|
conn->connector_id);
|
|
return -ENOENT;
|
|
}
|
|
|
|
/*
|
|
* modeset_create_fb(fd, dev): After we have found a crtc+connector+mode
|
|
* combination, we need to actually create a suitable framebuffer that we can
|
|
* use with it. There are actually two ways to do that:
|
|
* * We can create a so called "dumb buffer". This is a buffer that we can
|
|
* memory-map via mmap() and every driver supports this. We can use it for
|
|
* unaccelerated software rendering on the CPU.
|
|
* * We can use libgbm to create buffers available for hardware-acceleration.
|
|
* libgbm is an abstraction layer that creates these buffers for each
|
|
* available DRM driver. As there is no generic API for this, each driver
|
|
* provides its own way to create these buffers.
|
|
* We can then use such buffers to create OpenGL contexts with the mesa3D
|
|
* library.
|
|
* We use the first solution here as it is much simpler and doesn't require any
|
|
* external libraries. However, if you want to use hardware-acceleration via
|
|
* OpenGL, it is actually pretty easy to create such buffers with libgbm and
|
|
* libEGL. But this is beyond the scope of this document.
|
|
*
|
|
* So what we do is requesting a new dumb-buffer from the driver. We specify the
|
|
* same size as the current mode that we selected for the connector.
|
|
* Then we request the driver to prepare this buffer for memory mapping. After
|
|
* that we perform the actual mmap() call. So we can now access the framebuffer
|
|
* memory directly via the dev->map memory map.
|
|
*/
|
|
|
|
static int modeset_create_fb(int fd, struct modeset_dev *dev)
|
|
{
|
|
struct drm_mode_create_dumb creq;
|
|
struct drm_mode_destroy_dumb dreq;
|
|
struct drm_mode_map_dumb mreq;
|
|
int ret;
|
|
|
|
/* create dumb buffer */
|
|
memset(&creq, 0, sizeof(creq));
|
|
creq.width = dev->width;
|
|
creq.height = dev->height;
|
|
creq.bpp = 32;
|
|
ret = drmIoctl(fd, DRM_IOCTL_MODE_CREATE_DUMB, &creq);
|
|
if (ret < 0) {
|
|
fprintf(stderr, "cannot create dumb buffer (%d): %s\n",
|
|
errno, strerror(errno));
|
|
return -errno;
|
|
}
|
|
dev->stride = creq.pitch;
|
|
dev->size = creq.size;
|
|
dev->handle = creq.handle;
|
|
|
|
/* create framebuffer object for the dumb-buffer */
|
|
ret = drmModeAddFB(fd, dev->width, dev->height, 24, 32, dev->stride,
|
|
dev->handle, &dev->fb);
|
|
if (ret) {
|
|
fprintf(stderr, "cannot create framebuffer (%d): %s\n",
|
|
errno, strerror(errno));
|
|
ret = -errno;
|
|
goto err_destroy;
|
|
}
|
|
|
|
/* prepare buffer for memory mapping */
|
|
memset(&mreq, 0, sizeof(mreq));
|
|
mreq.handle = dev->handle;
|
|
ret = drmIoctl(fd, DRM_IOCTL_MODE_MAP_DUMB, &mreq);
|
|
if (ret) {
|
|
fprintf(stderr, "cannot map dumb buffer (%d): %s\n",
|
|
errno, strerror(errno));
|
|
ret = -errno;
|
|
goto err_fb;
|
|
}
|
|
|
|
/* perform actual memory mapping */
|
|
dev->map = mmap(0, dev->size, PROT_READ | PROT_WRITE, MAP_SHARED,
|
|
fd, mreq.offset);
|
|
if (dev->map == MAP_FAILED) {
|
|
fprintf(stderr, "cannot mmap dumb buffer (%d): %s\n",
|
|
errno, strerror(errno));
|
|
ret = -errno;
|
|
goto err_fb;
|
|
}
|
|
|
|
/* clear the framebuffer to 0 */
|
|
memset(dev->map, 0, dev->size);
|
|
|
|
return 0;
|
|
|
|
err_fb:
|
|
drmModeRmFB(fd, dev->fb);
|
|
err_destroy:
|
|
memset(&dreq, 0, sizeof(dreq));
|
|
dreq.handle = dev->handle;
|
|
drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* Finally! We have a connector with a suitable CRTC. We know which mode we want
|
|
* to use and we have a framebuffer of the correct size that we can write to.
|
|
* There is nothing special left to do. We only have to program the CRTC to
|
|
* connect each new framebuffer to each selected connector for each combination
|
|
* that we saved in the global modeset_list.
|
|
* This is done with a call to drmModeSetCrtc().
|
|
*
|
|
* So we are ready for our main() function. First we check whether the user
|
|
* specified a DRM device on the command line, otherwise we use the default
|
|
* /dev/dri/card0. Then we open the device via modeset_open(). modeset_prepare()
|
|
* prepares all connectors and we can loop over "modeset_list" and call
|
|
* drmModeSetCrtc() on every CRTC/connector combination.
|
|
*
|
|
* But printing empty black pages is boring so we have another helper function
|
|
* modeset_draw() that draws some colors into the framebuffer for 5 seconds and
|
|
* then returns. And then we have all the cleanup functions which correctly free
|
|
* all devices again after we used them. All these functions are described below
|
|
* the main() function.
|
|
*
|
|
* As a side note: drmModeSetCrtc() actually takes a list of connectors that we
|
|
* want to control with this CRTC. We pass only one connector, though. As
|
|
* explained earlier, if we used multiple connectors, then all connectors would
|
|
* have the same controlling framebuffer so the output would be cloned. This is
|
|
* most often not what you want so we avoid explaining this feature here.
|
|
* Furthermore, all connectors will have to run with the same mode, which is
|
|
* also often not guaranteed. So instead, we only use one connector per CRTC.
|
|
*
|
|
* Before calling drmModeSetCrtc() we also save the current CRTC configuration.
|
|
* This is used in modeset_cleanup() to restore the CRTC to the same mode as was
|
|
* before we changed it.
|
|
* If we don't do this, the screen will stay blank after we exit until another
|
|
* application performs modesetting itself.
|
|
*/
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
int ret, fd;
|
|
const char *card;
|
|
struct modeset_dev *iter;
|
|
|
|
/* check which DRM device to open */
|
|
if (argc > 1)
|
|
card = argv[1];
|
|
else
|
|
card = "/dev/dri/card0";
|
|
|
|
fprintf(stderr, "using card '%s'\n", card);
|
|
|
|
/* open the DRM device */
|
|
ret = modeset_open(&fd, card);
|
|
if (ret)
|
|
goto out_return;
|
|
|
|
/* prepare all connectors and CRTCs */
|
|
ret = modeset_prepare(fd);
|
|
if (ret)
|
|
goto out_close;
|
|
|
|
/* perform actual modesetting on each found connector+CRTC */
|
|
for (iter = modeset_list; iter; iter = iter->next) {
|
|
iter->saved_crtc = drmModeGetCrtc(fd, iter->crtc);
|
|
ret = drmModeSetCrtc(fd, iter->crtc, iter->fb, 0, 0,
|
|
&iter->conn, 1, &iter->mode);
|
|
if (ret)
|
|
fprintf(stderr, "cannot set CRTC for connector %u (%d): %s\n",
|
|
iter->conn, errno, strerror(errno));
|
|
}
|
|
|
|
/* draw some colors for 5seconds */
|
|
modeset_draw();
|
|
|
|
/* cleanup everything */
|
|
modeset_cleanup(fd);
|
|
|
|
ret = 0;
|
|
|
|
out_close:
|
|
close(fd);
|
|
out_return:
|
|
if (ret) {
|
|
errno = -ret;
|
|
fprintf(stderr, "modeset failed with error %d: %s\n", errno, strerror(errno));
|
|
} else {
|
|
fprintf(stderr, "exiting\n");
|
|
}
|
|
return ret;
|
|
}
|
|
|
|
/*
|
|
* A short helper function to compute a changing color value. No need to
|
|
* understand it.
|
|
*/
|
|
|
|
static uint8_t next_color(bool *up, uint8_t cur, unsigned int mod)
|
|
{
|
|
uint8_t next;
|
|
|
|
next = cur + (*up ? 1 : -1) * (rand() % mod);
|
|
if ((*up && next < cur) || (!*up && next > cur)) {
|
|
*up = !*up;
|
|
next = cur;
|
|
}
|
|
|
|
return next;
|
|
}
|
|
|
|
/*
|
|
* modeset_draw(): This draws a solid color into all configured framebuffers.
|
|
* Every 100ms the color changes to a slightly different color so we get some
|
|
* kind of smoothly changing color-gradient.
|
|
*
|
|
* The color calculation can be ignored as it is pretty boring. So the
|
|
* interesting stuff is iterating over "modeset_list" and then through all lines
|
|
* and width. We then set each pixel individually to the current color.
|
|
*
|
|
* We do this 50 times as we sleep 100ms after each redraw round. This makes
|
|
* 50*100ms = 5000ms = 5s so it takes about 5seconds to finish this loop.
|
|
*
|
|
* Please note that we draw directly into the framebuffer. This means that you
|
|
* will see flickering as the monitor might refresh while we redraw the screen.
|
|
* To avoid this you would need to use two framebuffers and a call to
|
|
* drmModeSetCrtc() to switch between both buffers.
|
|
* You can also use drmModePageFlip() to do a vsync'ed pageflip. But this is
|
|
* beyond the scope of this document.
|
|
*/
|
|
|
|
static void modeset_draw(void)
|
|
{
|
|
uint8_t r, g, b;
|
|
bool r_up, g_up, b_up;
|
|
unsigned int i, j, k, off;
|
|
struct modeset_dev *iter;
|
|
|
|
srand(time(NULL));
|
|
r = rand() % 0xff;
|
|
g = rand() % 0xff;
|
|
b = rand() % 0xff;
|
|
r_up = g_up = b_up = true;
|
|
|
|
for (i = 0; i < 50; ++i) {
|
|
r = next_color(&r_up, r, 20);
|
|
g = next_color(&g_up, g, 10);
|
|
b = next_color(&b_up, b, 5);
|
|
|
|
for (iter = modeset_list; iter; iter = iter->next) {
|
|
for (j = 0; j < iter->height; ++j) {
|
|
for (k = 0; k < iter->width; ++k) {
|
|
off = iter->stride * j + k * 4;
|
|
*(uint32_t*)&iter->map[off] =
|
|
(r << 16) | (g << 8) | b;
|
|
}
|
|
}
|
|
}
|
|
|
|
usleep(100000);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* modeset_cleanup(fd): This cleans up all the devices we created during
|
|
* modeset_prepare(). It resets the CRTCs to their saved states and deallocates
|
|
* all memory.
|
|
* It should be pretty obvious how all of this works.
|
|
*/
|
|
|
|
static void modeset_cleanup(int fd)
|
|
{
|
|
struct modeset_dev *iter;
|
|
struct drm_mode_destroy_dumb dreq;
|
|
|
|
while (modeset_list) {
|
|
/* remove from global list */
|
|
iter = modeset_list;
|
|
modeset_list = iter->next;
|
|
|
|
/* restore saved CRTC configuration */
|
|
drmModeSetCrtc(
|
|
fd,
|
|
iter->saved_crtc->crtc_id,
|
|
iter->saved_crtc->buffer_id,
|
|
iter->saved_crtc->x,
|
|
iter->saved_crtc->y,
|
|
&iter->conn,
|
|
1,
|
|
&iter->saved_crtc->mode
|
|
);
|
|
drmModeFreeCrtc(iter->saved_crtc);
|
|
|
|
/* unmap buffer */
|
|
munmap(iter->map, iter->size);
|
|
|
|
/* delete framebuffer */
|
|
drmModeRmFB(fd, iter->fb);
|
|
|
|
/* delete dumb buffer */
|
|
memset(&dreq, 0, sizeof(dreq));
|
|
dreq.handle = iter->handle;
|
|
drmIoctl(fd, DRM_IOCTL_MODE_DESTROY_DUMB, &dreq);
|
|
|
|
/* free allocated memory */
|
|
free(iter);
|
|
}
|
|
}
|
|
|
|
/*
|
|
* I hope this was a short but easy overview of the DRM modesetting API. The DRM
|
|
* API offers much more capabilities including:
|
|
* - double-buffering or tripple-buffering (or whatever you want)
|
|
* - vsync'ed page-flips
|
|
* - hardware-accelerated rendering (for example via OpenGL)
|
|
* - output cloning
|
|
* - graphics-clients plus authentication
|
|
* - DRM planes/overlays/sprites
|
|
* - ...
|
|
* If you are interested in these topics, I can currently only redirect you to
|
|
* existing implementations, including:
|
|
* - plymouth (which uses dumb-buffers like this example; very easy to understand)
|
|
* - kmscon (which uses libuterm to do this)
|
|
* - wayland (very sophisticated DRM renderer; hard to understand fully as it
|
|
* uses more complicated techniques like DRM planes)
|
|
* - xserver (very hard to understand as it is split across many files/projects)
|
|
*
|
|
* But understanding how modesetting (as described in this document) works, is
|
|
* essential to understand all further DRM topics.
|
|
*
|
|
* Any feedback is welcome. Feel free to use this code freely for your own
|
|
* documentation or projects.
|
|
*
|
|
* - Hosted on http://github.com/dvdhrm/docs
|
|
* - Written by David Herrmann <dh.herrmann@googlemail.com>
|
|
*/
|