KasperskyOS Community Edition 1.3

Memory-mapped I/O (mmio.h)

The API is defined in the header file sysroot-*-kos/include/coresrv/io/mmio.h from the KasperskyOS SDK.

The API is intended for working with MMIO memory. MMIO memory consists of physical addresses that are mapped to registers and memory of devices. (Portions of one physical address space are used for physical memory addressing and access to registers and memory of devices.)

Information about API functions is provided in the table below.

Using the API

The standard scenario for API usage includes the following steps:

  1. The MMIO memory region corresponding to the relevant device is registered.

    To complete this step, call the KnRegisterPhyMem() function.

    The handle of an MMIO memory region can be transferred to another process via IPC.

  2. Mapping the MMIO memory region to the memory of a process.

    An MMIO memory region can only be mapped to one virtual memory region of only one process at one time. (However, an MMIO memory region may be mapped to the virtual memory of different processes that own the handle of this region at different times.) Mapping allows the process to receive read-and/or-write access to the MMIO memory region.

    To reserve a virtual memory region and map the MMIO memory region to it, call the KnIoMapMem() function.

    A handle received when calling the KnIoMapMem() function cannot be transferred to another process via IPC.

  3. Reading data from the MMIO memory region and writing data to it via process memory.

    The 8-, 16- and 32-bit words that are read from the MMIO memory region or written to it are values of device registers or contents of device memory.

    To complete this step, use the IoReadMmReg8|16|32(), IoReadMmBuffer8|16|32(), IoWriteMmReg8|16|32(), and IoWriteMmBuffer8|16|32() functions.

Deregistering an MMIO memory region

To deregister an MMIO memory region, complete the following steps:

  1. Free the virtual memory region that was allocated when the KnIoMapMem() function was called.

    To complete this step, call the KnHandleClose() function and specify the handle that was received when the KnIoMapMem() function was called. (The KnHandleClose() function is declared in the header file sysroot-*-kos/include/coresrv/handle/handle_api.h from the KasperskyOS SDK.)

  2. Close or revoke each MMIO memory region handle in all processes that own these handles.

    To complete this step, use the KnHandleClose() and/or KnHandleRevoke() functions that are declared in the header file sysroot-*-kos/include/coresrv/handle/handle_api.h from the KasperskyOS SDK.

Information about API functions

mmio.h functions

Function(s)

Information about the function(s)

KnRegisterPhyMem()

Purpose

Registers an MMIO memory region.

Parameters

  • [in] addr – page-aligned base address of the MMIO memory region.
  • [in] size – size of the MMIO memory region. It must be a multiple of the memory page size.
  • [out] outRid – pointer to the handle of the MMIO memory region.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KnIoMapMem()

Purpose

Reserves the virtual memory region and maps the MMIO memory region to it.

Parameters

  • [in] rid – handle of the MMIO memory region.
  • [in] prot – flags defining the access rights to the virtual memory region. The flags are defined in the header file sysroot-*-kos/include/vmm/flags.h from the KasperskyOS SDK.
  • [in] attr – flags defining the cache settings. The flags are defined in the header file sysroot-*-kos/include/vmm/flags.h from the KasperskyOS SDK.
  • [out] addr – pointer to the base address of the virtual memory region.
  • [out] handle – pointer to the handle that is used to free the virtual memory region.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

Additional information

In the prot parameter, you can specify the following flags:

  • VMM_FLAG_READ – read access.
  • VMM_FLAG_WRITE – write access.

In the attr parameter, you can specify the following flags:

  • VMM_FLAG_WRITE_BACK, VMM_FLAG_WRITE_THROUGH, VMM_FLAG_WRITE_COMBINE, VMM_FLAG_CACHE_DISABLE – cache management.

    It is recommended to specify the VMM_FLAG_CACHE_DISABLE flag.

IoReadMmReg8()

IoReadMmReg16()

IoReadMmReg32()

Purpose

Get an 8-, 16- or 32-bit word from an MMIO memory region.

Parameters

  • [in] reg – virtual address for reading the word.

Returned values

8-, 16- or 32-bit word from the MMIO memory region.

IoReadMmBuffer8()

IoReadMmBuffer16()

IoReadMmBuffer32()

Purpose

Save a sequence of 8-, 16- or 32-bit words from the MMIO memory region to the buffer.

Parameters

  • [in] baseReg – virtual address of the first word in the sequence.
  • [out] dst – pointer to the buffer used to store the sequence of words.
  • [in] cnt – number of words in the sequence.

Returned values

N/A

Additional information

The functions can be used if the processor has an x86 or x86-64 architecture.

IoWriteMmReg8()

IoWriteMmReg16()

IoWriteMmReg32()

Purpose

Write an 8-, 16- or 32-bit word to an MMIO memory region.

Parameters

  • [in] reg – virtual address for writing the word.
  • [in] data – word to write.

Returned values

N/A

IoWriteMmBuffer8()

IoWriteMmBuffer16()

IoWriteMmBuffer32()

Purpose

Write a sequence of 8-, 16- or 32-bit words from the buffer to an MMIO memory region.

Parameters

  • [in] baseReg – virtual address for writing the sequence of words.
  • [in] src – pointer to the buffer containing the sequence of words.
  • [in] cnt – number of words in the sequence.

Returned values

N/A

Additional information

The functions can be used if the processor has an x86 or x86-64 architecture.