Kernel Graphics Interface


Kernel Graphics Interface (KGI) is the kernel side of the General Graphics Interface (GGI).


KGI was initially a Linux project with the goal of being a multi-OS subsystem. GGI was aiming to be the new generation Linux console subsystem alone. As GGI was progressing, it became clear that an explicit separation between kernel and user spaces was necessary. Steffen Seeger was maybe the most convinced of this: he rewrote KGI in 1998/1999 resulting in the version we have today both under Linux and FreeBSD.

KGI Project Website

It is being ported to FreeBSD in the kgi4BSD Project.

Purpose and Design

The KGI project aims at providing the kernel of modern Unixes with general purpose APIs (grouped as KgiAPI) for the implementation of:

KGI Device

A KGI device is not what we are used to think of e.g a peripheral or anything related to HW. Instead, a KGI device is a piece of code and data using / sharing KgiResources with other KGI devices.

Everything that uses the HW resources of a KgiDisplay (mode setting, framebuffer, HW pointers, palettes...) must register itself as a KGI device.

Maybe this layer of KGI was wrongly called devices because /dev/graphic and /dev/event which are the general KGI and KII user entrypoints are from Unix point of view called devices...

Different KGI devices are known currently:

The visible effect of mapping a KgiDevice to a KgiDisplay is the change of mode on that display. Before mapping a device, a mode must have been negociated with its display. When the device is mapped, the display is requested by KGI to set the mode previously negociated. When the device is unmapped, the display is asked to unset the mode before another device is mapped and so on.

Device mapping / unmapping is performed typically when:

The device methods (hooks) enable the device for example to redraw the screen when mapped or to block anything being performed on the display when unmapped.

See KgiDeviceAPI for details of programming a KgiDevice.

KGI Display

A KGI display is a graphic backend. It is an object composed of data and methods allocated at display registration. A display registration occurs for example when you load a KgimDriver.

Other kind of displays exists, kgi4BSD provides one based on the FreeBSD VESA driver.

See the KgiDisplayAPI for details about programming a KgiDisplay.


KGIM stands for Kernel Graphic Interface Module.

This is the API for writing graphic module drivers. KGIM is implemented in KGI as a KgiDisplay.

Drivers using the KgimAPI are known as KgimDrivers.

KGIM Drivers

A KGIM driver is the driver of a graphic board written for KGI and respecting the KgimAPI.

KGIM drivers are broken down in several subsystems (or subdrivers) which are typically the components of a graphic display HW chain:

The result of linking the compilation objects of the above subsystems gives you a KgimBoard. Currently, the glue-code between the subsystems is partly KGI API dependent for implementing a KgiDisplay and partly dependent of the operating system supposed to load the KgimBoard as a module.

A KGIM board is not yet a Plugins because it is specific to the OS it was compiled for.

Code which is system specific is isolated from the rest of the subsystems' code. This enables the compilation of the drivers in the same tree for any target OS. Also, KGI proposes a library of OS dependent routines for basic operations like mutexe management, delays, VM operations... The KGIM drivers use them instead of calling directly the OS API. These routines are part of the KgiAPI.

In the FreeBSD implementation, the board entity is a full FreeBSD kld module device driver, respecting the newbus interface, connected to the PCI core and responsible for dispatching the FreeBSD resources to the KGIM underlying clock, ramdac and chipset drivers (as they exist in the Linux implementation). The board driver probe/attach routines detect the chipset, prepare the KgiDisplay information and call the KGIM functions to powerup the KgimDrivers.

When the board module is loaded into the FreeBSD kernel, the probe routine has already acknowledged the chipset existance. Consequently, during the attach call, when the KGI chipset driver is powered up, the pci_find() routine is a nop instruction. Later, resource reservation like irq_claim and check_region are converted into FreeBSD bus_resource() calls.


KII stands for Kernel Input Interface. It is known as the kernel side of GII the General Input Interface (part of the GGI project).

It provides KgiInput with declarations and registrations routines to connect to a KgiFocus.

Currently, KII is on top of FreeBSD keyboard drivers. The main benefit is to get immediately FreeBSD keyboards supported by KII and KGI.

In the long term FreeBSD current kbd abstraction should be discardedand true KII drivers should be written.

The original kbd (atkbd or usbkbd) is a KII input connected to focus 0. Moused events are redirect to focus 0 by mean of another KII input (made by ioctls) and finally a KII client is given to provide the /dev/sysmouse interface to user apps. Hmm, little graphic:

/dev/consolectl -----------+
      ttyv1 <--------+     v
                     |- focus 0 <------ kbd
      ttyv2 <--------+     |
/dev/sysmouse <------------+

KGI Input

A KgiInput is basically anything capable of providing KII with events. KgiInput are typically input drivers that convert HW events (mouse move, buttons, key-press...) into KII events.

The KgiInput must register itself to KII using the KgiInputAPI. Once done, it may be polled by the KII engine or send itself events to KII.

KGI Focus

The KgiFocus is the central place in KGI where KgiInput are connected to KgiDisplay.

When you hit a key on your keyboard, you want it to go to the console you are working on and application(s) running on that console should get the event delivered.

Other applications running on other consoles should not get the event (for obvious security reasons).

When you want to switch to another physical (dual-head) or virtual (also known as VTs) console, you press a key combination to switch and then want now your chars to be sent to the right console.

In other words, you need the system manages the focus for you in a convenient, deterministic and safe manner.

These connections are at the same time dynamic and static. Static because the relationship between display and input instances is predefined. Dynamic because you can register any KgiInput (ps/2, usb mouse or keyboards) or KgiDisplay to the KgiFocus.

So what is predefined relationship? Let's have a look at the following:

 device 0 on focus 0, display 0, console 0
 device 1 on focus 0, display 0, console 1
 device 2 on focus 0, display 1, console 2
 device 3 on focus 0, display 1, console 3
 device 4 on focus 0, display 1, console 4
 device 5 on focus 0, display 1, console 5
 device 6 on focus 0, display 1, console 6
 device 7 on focus 0, display 1, console 7
 device 8 on focus 0, display 1, console 8
 device 9 on focus 0, display 1, console 9
 device 10 on focus 0, display 1, console 10
 device 11 on focus 0, display 1, console 11
 device 12 on focus 1, display 2, console 0
 device 13 on focus 1, display 2, console 1
 device 14 on focus 1, display 2, console 2
 device 15 on focus 1, display 2, console 3
 device 16 on focus 1, display 2, console 4
 device 17 on focus 1, display 2, console 5
 device 18 on focus 1, display 2, console 6
 device 19 on focus 1, display 2, console 7
 device 20 on focus 1, display 2, console 8
 device 21 on focus 1, display 2, console 9
 device 22 on focus 1, display 2, console 10
 device 23 on focus 1, display 2, console 11

It presents the case of 2 keyboards (represented by 2 focuses) for which one has 3 screens. Each focus can reach 12 consoles (typically what you get with a PC keyboard with F1..F12). Focus 1 is totally linked to display 2 (1 keyboard + 1 screen wired config) and focus 1 can either control consoles 0 and 1 through display 0 and others through display 1. It means that with focus 0, when you hit ALT+F1 you edit commands on display 0, ALT-F3 : you edit on display 1, ALT-F1 you change VT on display 0 and ALT-F4 to ALT-F12 : you switch to VTs on display 1.

KGI Mutex

Mutexes are the basic KGI execution control mechanism.

The mutex API was originaly design on the basis of FreeBSD mutex(9) and condvar(9) implementations. On FreeBSD, mutexes and convars are different objects. In the KGI implementation, I decided to merge them. It is mutex centric e.g some events may be associated to a mutex. It's convenient when you have a mutex locking a object, the events associated to the mutex are obviously related to the object locked.

For example, when a buffer is finished to execute, you want to own the buffer mutex when returning from the waiting queue. That way you can proceed on the buffer immediatly after the wait. The downside is that you must own the mutex before waiting...

See the KgiMutexAPI for detailed info.

KGI for FreeBSD (kgi4BSD) Project

kgi4BSD is a port of KGI for FreeBSD.

After the port of GGI to FreeBSD vgl(4) library at the of 2000, I decided to port KGI. Actually, KGI was my original challenge but what to do with KGI without GGI?

Before the kgi4BSD site was created, KGI/FreeBSD was a private project shared among KGI developers. Now kgi4BSD becomes more popular and this is a good thing. kgi4BSD is currently the only kernel graphic solution for the BSDs. It currently targets only FreeBSD but DragonfyBSD, OpenBSD and NetBSD (for its VM) could be planned.

It's a dual-licensed MIT-X / GPL project made 100% on spare time by very few people. If you like kernel development and graphics it's definitely a project for you ;)


We use the concept of condvar(9) with mutex(9) to implement something like the wait_event I found in KGI. I wonder why the condvar API is not part of the mutex one since a cv is nothing else than a mutex with a waitqueue. So I decided to merge FreeBSD condvar and mutexes in the KgiMutex implementation.

In KGI Linux code, page mapping is performed for a set of pages and not only on pagefault occurrence. I have to check whether this is also possible with FreeBSD VM. This is certainly possible using something like vm_fault_additional_pages() which is currently bypassed when getting OBJT_DEVICE or OBJT_KGI pages.

For KgiAccel, we have to change the protection of pages to provoke pagefaults on them. But pagefaults on resident pages is only expected to happen on COW pages. getpages is only used to allocation non resident pages and no flag in the prototype of getpages allow passing some flag. Faults on resident pages shall occur typically when a process being not mapped anymore attempts to access an KgiMmio or KgiAccel. By the way, pages must not be FICTITIOUS otherwise they have no corresponding PTE and thus the MMU entries can't be release by any means other than through pmaps! Currently I use my own page allocator and don't use the paging one. FreeBSD VM implementation seems completely inappropriate to alternative VM policies other than the paging one :(

What about adding a new method to pagers: .pgo_checkpages to handle faults on resident pages?

About SMP, I think excluding concurrent access to the kgi device structure could be a first attempt. Especially to manage atomic map / unmap of devices.

I'm wondering how to block a process performing graphics on a device that is unmapped. I'm not happy with any signal scheme and would really prefer a blocking thread. This implies some programming style, threading oriented with for example a thread for drawing (I mean accessing the HW resources) and others do the rest of the application. The main drawback is that programmers may not be aware of this and existing applications may not fit...


Application Programming Interface

KGI Device API


        kgi_u_t                 id;

        kgi_u_t                 dpy_id;

        kgi_device_flags_t      flags;

        kgi_mode_t              *mode;

        kgi_private_t           priv;

The three methods of a KGI device are callbacks called by KGI on KGI events:

        kgi_device_map_device_fn        *MapDevice;

        kgi_device_unmap_device_fn      *UnmapDevice;

        kgi_device_handle_event_fn      *HandleEvent;



        kii_input_t             *next;

        kii_u_t                 focus;

        kii_u_t                 id;

        kii_event_mask_t        events;

        kii_event_mask_t        report;

        kii_ascii_t             vendor[KII_MAX_VENDOR_STRING];
        kii_ascii_t             model[KII_MAX_MODEL_STRING];

        int     (*Command)(kii_input_t *, kii_u_t cmd, void *);

        int     (*Poll)(kii_input_t *);

        void    (*Parse)(kii_input_t *, kii_event_t *, int);

        kii_private_t           priv;


kii_register_input() is typically called when a new input driver is loaded.

kii_error_t kii_register_input(

 /* Comment: the focus to register to */
 kii_u_t focus,

 /* Comment: preinitialized data about the input to register */
 kii_input_t *dev

The return code is null (KII_EOK) on succes. Otherwise, the appropriate return code is given back.

A typical registration of an input is like this:

        snprintf(sc->kii_input.vendor, KII_MAX_VENDOR_STRING, "KII FreeBSD keyboard");
        snprintf(sc->kii_input.model, KII_MAX_VENDOR_STRING, kbd->kb_name);
        sc-> = KII_EM_KEY | KII_EM_RAW_DATA;
        sc-> = KII_EM_KEY | KII_EM_RAW_DATA;
        sc->kii_input.priv.priv_ptr = sc;
        sc->kii_input.Poll = kbdriver_poll;
        sc->kii_input.Parse = kbdriver_parser;

        kii_error = kii_register_input((~0), &sc->kii_input);

In the above example, KII_INVALID_FOCUS is passed as focus id to let KII auto register the input according to its capabilities e.g sc-> sc->kii_input.priv.priv_ptr is initialized with the software data pointer of the kbd device.

kbdriver_parser() takes scancodes from the RAW output of a kbd and fills the event structure passed in. Event is only reported to KII if the event type corresponds (e.g bit set) in sc->

Here is another example of registration:

        snprintf(mouse->input.vendor, KII_MAX_VENDOR_STRING, "FreeBSD");
        snprintf(mouse->input.model, KII_MAX_VENDOR_STRING,  "Syscons Emulation Mouse");
        mouse->input.focus = KII_INVALID_FOCUS;
        mouse-> = KII_INVALID_DEVICE;
        mouse-> = KII_EM_POINTER & ~KII_EM_PTR_ABSOLUTE;
        mouse->input.priv.priv_ptr = mouse;
        /* XXX Force registration to focus 0 */
        if (!(error = kii_register_input(0, &mouse->input))) {
                make_dev(&scectl_cdevsw, SC_CONSOLECTL,
                         UID_ROOT, GID_WHEEL, 0600, "consolectl");
        } else {
                KRN_ERROR("Could not register sce_mouse");

The above registration comes from the scemul device provided by KII to FreeBSD. It emulates the /dev/consolectl interface to which the general purpose mouse daemon sends data on mouse events. This data is processed internally and sent back to userland by /dev/sysmouse or /dev/event.

In this example, no Parser or Poll methods are set. Instead, scemul responds to the mouse daemon inputs by sending directly the event to KII.


kii_unregister_input() is typically called when the module of an input driver is unloaded.

void kii_unregister_input(

 /* Comment: the input to unregister from its focus */
 kii_input_t *dev

KGI Display API


Data of a KGI display are:

        kgi_u_t         revision;

        kgi_ascii_t     vendor[KGI_MAX_VENDOR_STRING];
        kgi_ascii_t     model[KGI_MAX_VENDOR_STRING];

        kgi_u32_t       flags;

        kgi_u_t         mode_size;

        kgi_mode_t      *mode;

        kgi_u_t id;

        kgi_u_t graphic;

        struct kgi_display_s *prev;

        struct kgi_device_s             *focus;

The methods of a KGI display are:

        kgi_display_refcount_fn         *IncRefcount;

        kgi_display_refcount_fn         *DecRefcount;

        kgi_display_check_mode_fn       *CheckMode;

        kgi_display_set_mode_fn         *SetMode;

        kgi_display_set_mode_fn         *UnsetMode;

        kgi_display_command_fn          *Command;


A display must register itself to offer its services to the KgiDevice.

kgi_s_t kgi_register_display(

 /* Comment: the preinitialized display to register */
 kgi_display_t *dpy,

 /* Comment: the suggested id for the display */
 kgi_u_t id

A registration typically occur when loading a display module. The display must be initialized before registration: at least one KgiImage must be defined and the display resources must be described.

For example, for the FreeBSD native VESA display:

        dpy->revision = KGI_DISPLAY_REVISION;
        snprintf(dpy->vendor, KGI_MAX_VENDOR_STRING, "KGI FreeBSD");
        snprintf(dpy->model, KGI_MAX_VENDOR_STRING, "dpysw");

No specific flag and the display mode specific area is sizeof(vidsw_mode_t) long.

        dpy->flags = 0;
        dpy->mode_size = sizeof(vidsw_mode_t);

with (dpm is mandatory here IIRC):

typedef struct vidsw_mode_s {

        kgi_dot_port_mode_t dpm;

        video_info_t mode_info;
        video_info_t oldmode_info;

} vidsw_mode_t;

The display methods are then initialized with yet not device focused:

        mode = &sc->mode;

        dpy->mode = mode;
        dpy->id = -1;
        dpy->graphic = 0;
        dpy->IncRefcount = dpysw_inc_refcount;
        dpy->DecRefcount = dpysw_dec_refcount;

        dpy->CheckMode = dpysw_check_mode;
        dpy->SetMode = dpysw_set_mode;
        dpy->UnsetMode = dpysw_unset_mode;
        dpy->Command = dpysw_display_command;

        dpy->focus = NULL;

        mode->revision          = KGI_MODE_REVISION;
        mode->dev_mode          = NULL;
        mode->images            = 1;
        mode->img[0].out        = NULL;
        mode->img[0].flags      = KGI_IF_TEXT16;
        mode->img[0].virt.x     = adp->va_info.vi_width;
        mode->img[0].virt.y     = adp->va_info.vi_height;
        mode->img[0].size.x     = adp->va_info.vi_width;
        mode->img[0].size.y     = adp->va_info.vi_height;
        mode->img[0].frames     = 1;
        mode->img[0].tluts      = 0;
        mode->img[0].aluts      = 0;
        mode->img[0].ilutm      = 0;
        mode->img[0].alutm      = 0;
        mode->img[0].fam        = 0;
        mode->img[0].cam        = 0;

The framebuffer resource is described that way:

        fb->meta                = dpy;
        fb->type                = KGI_RT_MMIO_FRAME_BUFFER;
        fb->prot                = KGI_PF_APP_RWS | KGI_PF_LIB_RWS | KGI_PF_DRV_RWS;
        fb->name                = "Frame buffer";
        fb->access              = 8 + 16 + 32 + 64;
        fb->align               = 8 + 16;
        fb->win.size            = (kgi_size_t)adp->va_window_size;
        fb->win.virt            = (kgi_virt_addr_t)adp->va_window;
        fb->win.bus             = (kgi_bus_addr_t)0;
        fb->win.phys            = (kgi_phys_addr_t)adp->va_mem_base;
        fb->size                = (kgi_size_t)adp->va_mem_size;
        fb->offset              = 0;
        fb->SetOffset           = dpysw_set_offset;


void    kgi_unregister_display(

 /* Comment: the display to unregister */
 kgi_display_t *dpy


kgi_display_registered shall be used to know if a display id is already bound to a registered display.

kgi_s_t kgi_display_registered(

 /* Comment: the display id */
 kgi_u_t id

Returns KGI_EOK if bound, KGI_ENODEV otherwise.


The KgimAPI is part of KGI but strictly speaking, it is a lower level API interfaced to KGI thanks to the KgiDisplayAPI.



The kgi_mutex_t is an opaque structure. One shall use it with the following methods and never access it directly. It is OS specific and its content, its size may vary from one platform to another.


Allocate a mutex. mtx must be null. The return parameter is 0 (KGI_EOK) in case of success.

If the mutex has to be owned in the context of event synchronisation mechanisms, the number of different events has to be specified, otherwise KGI_MUTEX_NOEVENT shall be passed. Anyway nb_event can't exceed the maximum number KGI_MUTEX_MAXEVENT.

kgi_u_t kgi_mutex_alloc(

 /* Comment: pointer to the mutex to initialize */
 kgi_mutex_t *mtx,

 /* Comment: max number of events */
 kgi_u_t nb_events


Free a mutex. The mutex must have be done before free. mtx is null when returning from this function. Memory allocated for event management is also freed.

void kgi_mutex_free(

 /* Comment: pointer to the mutex to free */
 kgi_mutex_t *mtx


Initialize a pre-allocated mutex. The name passed is used for debug etc.

void kgi_mutex_init(

 /* Comment: pointer to the mutex to initialize */
 kgi_mutex_t *mtx,

 /* Comment: name of the mutex */
 const char *name


Release a mutex. Memory allocated for the mutex itself is not freed.

void kgi_mutex_done(

 /* Comment: pointer to the mutex to release */
 kgi_mutex_t *mtx


Lock the mutex. If the mutex is not free, the thread is blocked.

void kgi_mutex_lock(

 /* Comment: pointer to the mutex to lock */
 kgi_mutex_t *mtx


Unlock the mutex previously locked.

void kgi_mutex_unlock(

 /* Comment: pointer to the mutex to unlock */
 kgi_mutex_t *mtx


Wait on event e.g unlock the mutex and add the thread to the list of waiting threads on this event. When the thread is made runnable again, it owns the mutex.

Important: the mutex must be already locked by the current running thread when calling kgi_mutex_wait().

Callers are responsible for managing the relationship between event ids and their signification. the event parameter can't exceed nb_events passed when the mutex was initialized.

void kgi_mutex_wait(

 /* Comment: pointer to the mutex hold */
 kgi_mutex_t *mtx,

 /* Comment: the event to wait on */
 kgi_u_t event


Wakeup threads waiting on an event to occur with mutex ownership. If unblock_all is TRUE then unblock all waiters otherwise only one.

void kgi_mutex_signal(

 /* Comment: pointer to the mutex processes are blocked on */
 kgi_mutex_t *mtx,

 /* Comment: event to signal */
 kgi_u_t event,

 /* Comment: 1 = unblock all processes waiting on the mutex, 0 = unblock only the next one */
 int unblock_all

CategoryStale CategoryInactiveProject CategoryHistorical

NicholasSouchu/KernelGraphicsInterface (last edited 2022-06-10T02:46:39+0000 by KubilayKocak)