Managing processes (low-level API task_api.h)

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

Main capabilities of the API:

Information about API functions is provided in the table below.

The libkos library also provides a high-level API for process management. It is recommended to use this specific API. The low-level API should be used only if the high-level API does not have sufficient capabilities.

Creating and starting a process

The following API functions are required for creating and running a process:

These functions are intended for use by the libkos library. The solution developer must use the task.h API functions to create and run processes. For many functions of the task_api.h API that are intended for use by the solution developer, the input parameter is the process handle. To get the handle of a process, call the KosTaskGetHandle() function from the task.h API. The handle of a process can be transferred to another process via IPC.

The KnTaskVmReserveForElf() function is intended for use both by the libkos library and by the solution developer. This function reserves a region of virtual memory for loading the ELF image. The task and elfDescr input parameters are used to accept the process handle and the pointer to the object describing the ELF image, respectively. (The object describing the ELF image is created by the KnElfOpen() function from the elf_api.h API.) The relocBase output parameter is used to pass the ELF image load offset. If the ELF file has the DYN type, it passes a random offset. It passes a zero offset for an ELF file of a different type. The ELF image load offset is an input parameter of the KosTaskLoadSegments() and KosTaskInitFromSegEx() functions from the task.h API and of the KnElfCreateVmSegEx() function from the elf_api.h API. A random ELF image load offset is part of the address space layout randomization (ASLR) support. Address Space Layout Randomization (ASLR) is the use of random addresses for the location of data structures (ELF image, dynamic libraries, stack and heap) in process memory to make it harder to exploit vulnerabilities associated with a conventional process address space structure that is known by a hacker in advance.

Before a process is started, it receives data from its parent process via a static connection page. A static connection page (SCP) is a set of structures containing data for statically creating IPC channels, startup parameters, and environment variables of a program. A parent process writes data to the SCP of a child process by calling the KnTaskSetEnv() function. When a child process is started, it reads data from the SCP by calling the KnTaskGetEnv() function, then it deletes the SCP by calling the KnTaskFreeEnv() function. Like the KnTaskSetEnv() function, the KnTaskGetEnv() and KnTaskFreeEnv() functions are intended for use by the libkos library.

By default, the initial thread of a process is a standard thread. (For details about thread scheduler classes, see Managing threads (low-level API thread_api.h) To get the priority of the initial thread of a process, call the KnTaskGetInitialThreadPriority() function. To change the priority of the initial thread of a process, call the KnTaskSetInitialThreadPriority() function. To change the scheduler class and/or priority of the initial thread of a process, call the KnTaskSetInitialPolicy() function. All three functions can be used after setting the program entry point and after starting the process. The program entry point is set by all the process creation functions from the task.h API, except for KosTaskCreate(). If the process is created by the KosTaskCreate() function, the entry point will be set after calling the KosTaskLoadSegments() function.

Terminating a process

Process termination includes the following:

A process can be terminated for the following reasons:

Process exit codes are defined by the solution developer. These codes must be specified in the status parameter of the KnTaskExit() function. If a process was terminated due to the termination of all its threads, the exit code of this process will be the exit code of its initial thread. To get the exit code of a process that was terminated on its own initiative, call the KnTaskGetExitCode() function.

To get information regarding the reason for process termination, call the KnTaskGetExitStatus() function. This function determines whether a process was terminated on its own initiative, by external request, or unexpectedly.

To get information about an unhandled exception that led to an unexpected termination of a process, call the KnTaskGetExceptionInfo() function. This information includes the exception code and the context of the thread in which this exception occurred.

If the process was created by one of the functions of the task.h API with the specified KOS_TASK_FLAG_DUMPABLE flag, an unhandled exception will cause this process to go into a "frozen" state and the process will not terminate. When a process is switched to a "frozen" state, its threads are terminated as a result of the unhandled exception but its resources are not freed, which means that you can get information about this process. To get the context of a thread that is part of a frozen process, call the KnTaskGetThreadContext() function. To get information about the virtual memory region that belongs to a frozen process, call the KnTaskGetNextVmRegion() function. This information includes the base address and size of the virtual memory region, and the access rights to this virtual memory region. Before a process switches to the frozen state, stack backtrace data (call stack information) is printed for the thread in which the unhandled exception occurred. To terminate a frozen process, call the KnTaskTerminateAfterFreezing() function.

To ensure that the kernel object describing a process is deleted after this process is terminated, each of its handles must be closed or revoked in all processes that own these handles before or after this process is terminated.

Handling exceptions

To register an exception handling function for a process, call the KnTaskSetExceptionHandler() function from this process. This function deregisters the previous exception handling function and returns its ID. By saving this ID, you can subsequently register the previous exception handling function again.

An exception handling function is called when an exception occurs in any process thread. If the exception is successfully handled, this function returns a value other than zero. Otherwise it returns zero. The input parameter of the exception handling function is a structure containing information about the exception. The type of this structure is defined in the header file sysroot-*-kos/include/thread/tcbpage.h from the KasperskyOS SDK.

If an exception handling function registered at the process level failed to successfully handle an exception, the exception handling function registered at the level of the thread in which the exception occurred will be called. (For details about handling exceptions at the thread level, see Managing threads (low-level API thread_api.h)).

Receiving information about a process

To get the process ID (PID), call the KnTaskGetId() or KnTaskGetIdByHandle() function. The KnTaskGetId() function lets you get the PID of the calling process. The KnTaskGetIdByHandle() function lets you get the PID of a process based on its handle.

To get the name of a process, call the KnTaskGetName() function from this process.

To get the path to the executable file from which the process was created, call the KnTaskGetPath() function from this process.

Transferring handles to a child process

A parent process can transfer handles to a child process that is not yet running. (General information about transferring handles is provided in the Transferring handles section.) The KnTaskTransferResource() function lets you transfer a handle to a child process. This function is intended for use by the libkos library. The solution developer needs to use the KosTaskTransferResource() function from the task.h API, which not only transfers the handle, but also copies it to the SCP of the child process so that the child process can find the transferred handle by calling the KosTaskLookupResource() function from the task.h API.

Reserving memory in a child process

The API includes the KnTaskVmReserve() and KnTaskVmFree() functions, which reserve and free virtual memory regions, respectively, in a child process that does not yet have a defined program entry point. These functions are intended for use by the libkos library.

Getting the GSI address

Global system information (GSI) is a structure containing system information, such as the timer count since the kernel started, the timer counts per second, the number of processors (processor cores) in active state, and processor cache data. The type of structure is defined in the header file sysroot-*-kos/include/task/pcbpage.h from the KasperskyOS SDK. The KnTaskGetGsi() function gets the GSI address. This function is intended for use by the libc library.

Getting the PCB address

A process control block (PCB) is a structure containing process information that is used by the kernel to manage this process. The type of structure is defined in the header file sysroot-*-kos/include/task/pcbpage.h from the KasperskyOS SDK. The KnTaskGetPcb() function gets the PCB address. This function is intended for use by the libc library.

Getting the addresses and sizes of the symbol table.symtab and the string table .strtab

The KnTaskGetElfSyms() function lets you get the addresses and sizes of the symbol table .symtab and the string table .strtab loaded into the process memory. This function is intended for a mechanism that prints stack backtrace data when operating in user mode (not in kernel mode).

Information about API functions

task_api.h functions

Function

Information about the function

KnTaskCreate()

Purpose

Creates a process.

Parameters

  • [in] name – pointer to the process name.
  • [in] eiid – pointer to the process class name.
  • [in,optional] path – pointer to the executable file path, or RTL_NULL if you do not need to specify the path to the executable file. This function does not use the specified path to query the executable file but instead only saves it as metadata of the created process.
  • [in,optional] stackSize – thread stack limit (in bytes) used by default when creating process threads. If 0 is specified, the stack will be 1 MB by default.
  • [in] priority – priority of the initial thread. The parameter type is defined in the header file sysroot-*-kos/include/coresrv/thread/thread_api.h from the KasperskyOS SDK.
  • [out] outHandle – pointer to the process handle.

Returned values

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

KnTaskCreateEx()

Purpose

Creates a process.

Parameters

  • [in] name – pointer to the process name.
  • [in] eiid – pointer to the process class name.
  • [in,optional] path – pointer to the executable file path, or RTL_NULL if you do not need to specify the path to the executable file. This function does not use the specified path to query the executable file but instead only saves it as metadata of the created process.
  • [in,optional] stackSize – thread stack limit (in bytes) used by default when creating process threads. If 0 is specified, the stack will be 1 MB by default.
  • [in] priority – priority of the initial thread. The parameter type is defined in the header file sysroot-*-kos/include/coresrv/thread/thread_api.h from the KasperskyOS SDK.
  • [in] flags – flags defining the parameters for creating the process.
  • [out] outHandle – pointer to the process handle.

Returned values

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

Additional information

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

  • TaskExceptionTerminatesTask – the process is terminated as a result of an unhandled exception. This flag cannot be specified together with other flags except the TaskEmpty flag.
  • TaskExceptionFreezesTask – the process switches to the "frozen" state as a result of an unhandled exception. This flag cannot be specified together with other flags except the TaskEmpty flag.
  • TaskExceptionTerminatesSystem – the hardware platform is shut down or restarted as a result of an unhandled exception in the process. This flag cannot be specified together with other flags except the TaskEmpty flag. The TaskExceptionTerminatesSystem flag may be unavailable for use depending on the specific configuration of the KasperskyOS kernel. If this flag is available, the kernel configuration also defines the optional response to an unhandled exception in a process: shutdown or restart.
  • TaskEmpty – creates an "empty" process. This mandatory flag must be specified.

KnTaskGetGsi()

Purpose

Gets the GSI address for the calling process.

Parameters

N/A

Returned values

Pointer to the GSI. The data type for GSI storage is defined in the header file sysroot-*-kos/include/task/pcbpage.h from the KasperskyOS SDK.

KnTaskGetPcb()

Purpose

Gets the address of the process control block (PCB) for the calling process.

Parameters

N/A

Returned values

Pointer to the PCB. The data type for PCB storage is defined in the header file sysroot-*-kos/include/task/pcbpage.h from the KasperskyOS SDK.

KnTaskGetEnv()

Purpose

Gets the SCP address of the calling process.

Parameters

  • [out] envSize – pointer to the size of the SCP (in bytes).

Returned values

Pointer to the SCP, or RTL_NULL if the process does not have an SCP.

KnTaskSetEnv()

Purpose

Writes data to the SCP of a process.

Parameters

  • [in] task – process handle.
  • [in] env – pointer to the buffer containing data to be written to the SCP.
  • [in] envSize – size (in bytes) of the data to be written to the SCP.

Returned values

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

KnTaskFreeEnv()

Purpose

Deletes the SCP of the calling process.

Parameters

N/A

Returned values

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

KnTaskResume()

Purpose

Resumes a process.

Parameters

  • [in] task – process handle.

Returned values

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

KnTaskExit()

Purpose

Terminates the calling process.

Parameters

  • [in] status – exit code of the process.

Returned values

Error code.

Additional information

Does not terminate the process as long as it contains threads attached to interrupts.

KnTaskTerminate()

Purpose

Terminates a process.

Parameters

  • [in] task – process handle.

Returned values

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

KnTaskGetExitStatus()

Purpose

Gets information about the reason for process termination.

Parameters

  • [in] task – handle of the terminated process.
  • [out] status – pointer to the value indicating the reason for process termination. The data type for storing this value is defined in the header file sysroot-*-kos/include/task/pcbpage.h from the KasperskyOS SDK.

Returned values

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

Additional information

You can use the status parameters to get the following values:

  • TaskExitUnexpected – the process terminated unexpectedly.
  • TaskExitNormal – the process terminated on its own initiative.
  • TaskExitTerminated – the process was terminated by an external request.

KnTaskGetExceptionInfo()

Purpose

Gets information about an unhandled exception that led to an unexpected termination of a process.

Parameters

  • [in] task – handle of the unexpectedly terminated process.
  • [out] excType – pointer to an exception code that is non-specific for any processor architecture. The data type for storing this code is defined in the header file sysroot-*-kos/include/hal/exc_codes.h from the KasperskyOS SDK.
  • [out] excNo – pointer to an exception code that is specific to the utilized processor architecture. The data type for storing this code is defined in the header file sysroot-*-kos/include/hal/exc_codes.h from the KasperskyOS SDK.
  • [out] exceptionContext – pointer to the structure containing the context of the thread in which the exception occurred. The type of structure is defined in the header file sysroot-*-kos/include/hal/*/frame.h from the KasperskyOS SDK.

Returned values

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

KnTaskGetThreadContext()

Purpose

Gets the context of a thread that is part of a frozen process.

Parameters

  • [in] task – handle of the frozen process.
  • [in] index – thread index. It is used to enumerate threads. Enumeration starts with zero. A thread in which an unhandled exception occurred has a zero index.
  • [out] context – pointer to the structure containing the thread ID (TID) and context of the thread. The type of structure is defined in the header file sysroot-*-kos/include/thread/context.h from the KasperskyOS SDK.

Returned values

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

KnTaskGetNextVmRegion()

Purpose

Gets information about the virtual memory region that belongs to a frozen process.

Parameters

  • [in] task – handle of the frozen process.
  • [in,optional] after – address after which the virtual memory region was placed, or RTL_NULL if you need to receive information about the virtual memory region that was placed first.
  • [out] next – pointer to the base address of the virtual memory region.
  • [out] size – pointer to the size of the virtual memory region (in bytes).
  • [out] flags – pointer to the flags indicating the parameters of the virtual memory region. The flags are defined in the header file sysroot-*-kos/include/vmm/flags.h from the KasperskyOS SDK.
  • [out,optional] outHandle – pointer to the handle of the MDL buffer mapped to the virtual memory region. Creates an MDL buffer from physical memory mapped to a virtual memory region (even if there is already an MDL buffer mapped to this virtual memory region). If you specify RTL_NULL, an MDL buffer will not be created.

Returned values

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

KnTaskTerminateAfterFreezing()

Purpose

Terminates a frozen process.

Parameters

  • [in] task – handle of the frozen process.

Returned values

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

KnTaskGetExitCode()

Purpose

Gets the exit code of a process that terminated on its own initiative.

Parameters

  • [in] task – handle of the terminated process.
  • [out] exitCode – pointer to the process exit code.

Returned values

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

KnTaskGetId()

Purpose

Gets the process ID (PID) for the calling process.

Parameters

N/A

Returned values

Process ID. The ID type is defined in the header file sysroot-*-kos/include/task/pidtype.h from the KasperskyOS SDK.

KnTaskGetName()

Purpose

Gets the name of a calling process.

Parameters

  • [out] name – pointer to the buffer used to store the process name.
  • [in] msize – size of the buffer used to store the process name (in bytes).

Returned values

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

KnTaskGetPath()

Purpose

Gets the path to the executable file from which the calling process was created.

Parameters

  • [out] path – pointer to the buffer used to store the path to the executable file. If the path to the executable file was not specified when the process was created, an empty string will be saved in the buffer.
  • [in] msize – size (in bytes) of the buffer used to store the path to the executable file.

Returned values

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

KnTaskGetInitialThreadPriority()

Purpose

Gets the priority of the initial thread of a process.

Parameters

  • [in] task – process handle.
  • [out] priority – pointer to the priority of the initial thread of the process. The data type for storing the priority of a thread is defined in the header file sysroot-*-kos/include/coresrv/thread/thread_api.h from the KasperskyOS SDK.

Returned values

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

KnTaskSetInitialThreadPriority()

Purpose

Sets the priority of the initial thread of a process.

Parameters

  • [in] task – process handle.
  • [in] priority – priority of the initial thread of a process. The parameter type is defined in the header file sysroot-*-kos/include/coresrv/thread/thread_api.h from the KasperskyOS SDK.

Returned values

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

KnTaskSetExceptionHandler()

Purpose

Registers the exception handling function for the calling process.

Parameters

  • [in,optional] handler – ID of the exception handling function, or RTL_NULL to deregister the previously registered function without registering a new one. The parameter type is defined in the header file sysroot-*-kos/include/task/pcbpage.h from the KasperskyOS SDK.

Returned values

ID of the previously registered exception handling function if one exists, otherwise RTL_NULL. The type of returned value is defined in the header file sysroot-*-kos/include/task/pcbpage.h from the KasperskyOS SDK.

KnTaskLoadSeg()

Purpose

Loads an ELF image segment into process memory.

Parameters

  • [in] h – process handle.
  • [in] seg – pointer to the structure describing the loaded ELF image segment. If the loadAddr field of this structure is set equal to 0, the segment load address will be selected automatically. The type of structure is defined in the header file sysroot-*-kos/include/coresrv/vmm/vmm_types.h from the KasperskyOS SDK.
  • [out] actual – pointer to the load address of the ELF image segment.

Returned values

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

KnTaskVmReserve()

Purpose

Reserves a virtual memory region in a process.

Parameters

  • [in] task – process handle.
  • [in,optional] hint – page-aligned, preferred base address of the virtual memory region, or RTL_NULL to select this address automatically.
  • [in] size – size (in bytes) of the virtual memory region.
  • [in] flags – flags defining the parameters of the virtual memory region. 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 reserved virtual memory region.

Returned values

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

Additional information

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

  • VMM_FLAG_LOCKED – commits the virtual memory region while allocating the entire required volume of physical memory. When the KnTaskVmReserve() function is called with this flag, the memory is not committed. This flag affects subsequent commitment-related actions taken on the reserved virtual memory region.
  • VMM_FLAG_READ, VMM_FLAG_WRITE, VMM_FLAG_EXECUTE and VMM_FLAG_RWX_MASK – flags defining the access rights to the virtual memory region.
  • VMM_FLAG_LOW_GUARD, VMM_FLAG_HIGH_GUARD – adds a guard page to the beginning and end of the virtual memory region, respectively. The size of the guard page is not included in the size of the virtual memory region.

Permissible combinations of flags defining the access rights to the virtual memory region:

  • VMM_FLAG_READ – read access.
  • VMM_FLAG_READ | VMM_FLAG_WRITE – read-and-write access.
  • VMM_FLAG_READ | VMM_FLAG_EXECUTE – read-and-execute access.
  • VMM_FLAG_RWX_MASK or VMM_FLAG_READ | VMM_FLAG_WRITE | VMM_FLAG_EXECUTE – read/write/execute access.

KnTaskVmReserveForElf()

Purpose

Reserves a virtual memory region in a process for loading the ELF image.

Parameters

  • [in] task – process handle.
  • [in] elfDescr – pointer to the object describing the ELF image. The data type for storing the object is defined in the header file sysroot-*-kos/include/coresrv/elf/elf_api.h from the KasperskyOS SDK.
  • [out] relocBase – pointer to the ELF image load offset (in bytes).

Returned values

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

KnTaskVmFree()

Purpose

Frees the virtual memory region that was reserved by calling the KnTaskVmReserve() function.

Parameters

  • [in] task – process handle.
  • [in] base – page-aligned base address of the virtual memory region.
  • [in] size – size (in bytes) of the virtual memory region.

Returned values

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

KnTaskSetInitialState()

Purpose

Defines the program entry point and the ELF image load offset.

Parameters

  • [in] h – process handle.
  • [in] state – pointer to the structure containing the address of the program entry point and the ELF image load offset (in bytes).

Returned values

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

KnTaskLoadElfSyms()

Purpose

Loads the symbol table .symtab and string table .strtab into process memory.

Parameters

  • [in] h – process handle.
  • [in] symTabSeg is a pointer to the structure describing the loaded ELF image segment with the symbol table .symtab. The type of structure is defined in the header file sysroot-*-kos/include/coresrv/vmm/vmm_types.h from the KasperskyOS SDK.
  • [in] symTabSize – size of the symbol table .symtab (in bytes).
  • [in] strTabSeg is a pointer to the structure describing the loaded ELF image segment with the string table .strtab. The type of structure is defined in the header file sysroot-*-kos/include/coresrv/vmm/vmm_types.h from the KasperskyOS SDK.
  • [in] strTabSize – size of the string table .strtab (in bytes).

Returned values

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

KnTaskSetInitialPolicy()

Purpose

Defines the scheduler class and priority of the initial thread of a process.

Parameters

  • [in] task – process handle.
  • [in] policy – scheduler class of the initial thread of the process. The parameter type is defined in the header file sysroot-*-kos/include/thread/tidtype.h from the KasperskyOS SDK.
  • [in] priority – priority of the initial thread of a process. The parameter type is defined in the header file sysroot-*-kos/include/coresrv/thread/thread_api.h from the KasperskyOS SDK.
  • [in,optional] param – pointer to the parameters of the initial thread scheduler class, or RTL_NULL. The data type for storing these parameters is defined in the header file sysroot-*-kos/include/coresrv/thread/thread_api.h from the KasperskyOS SDK.

Returned values

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

Additional information

In the param parameter, you can specify RTL_NULL if you need to define the default parameters for the scheduler class of the initial thread of the process.

In the param parameter, you must specify RTL_NULL if the scheduler class of the initial thread of the process does not have any parameters.

KnTaskReseedAslr()

Purpose

Defines the seed value for ASLR support in the defined process.

Parameters

  • [in] task – process handle.
  • [in] seed – pointer to the buffer containing the seed value of the random number generator.
  • [in] seedSize – size (in bytes) of the buffer containing the seed value of the random number generator.

Returned values

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

KnTaskGetElfSyms()

Purpose

Gets the addresses and sizes of the symbol table .symtab and string table .strtab for the calling process.

Parameters

  • [out] relocBase – pointer to the ELF image load offset (in bytes). Add the ELF image load offset to the address of a symbol from the symbol table .symtab to get the address of this symbol in process memory.
  • [out] syms – pointer to the address of the symbol table .symtab.
  • [out] symsCnt – pointer to the size of the symbol table .symtab (in bytes).
  • [out] strs – pointer to the address of the string table .strtab.
  • [out] strsCnt – pointer to the size of the string table .strtab (in bytes).

Returned values

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

Additional information

If the symbol table .symtab and string table .strtab are not loaded into process memory by calling the KnTaskLoadElfSyms() function, the KnTaskGetElfSyms() function returns rcOk (if there are no other errors). In this case, the received addresses of the tables have RTL_NULL values, and the received sizes of the tables are zero.

KnTaskGetIdByHandle()

Purpose

Gets the process ID (PID).

Parameters

  • [in] task – process handle.
  • [out] taskId – pointer to the process ID. The ID type is defined in the header file sysroot-*-kos/include/task/pidtype.h from the KasperskyOS SDK.

Returned values

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

KnTaskPanic()

Purpose

Initiates an exception that cannot be handled and leads to the process terminating.

Parameters

N/A

Returned values

N/A

KnTaskTransferResource()

Purpose

Transfers a handle to a process that is not yet running.

Parameters

  • [in] task – process handle.
  • [in] srcHandle – transferred handle.
  • [in] srcBadge – handle of the resource transfer context object.
  • [in] dstRights – permissions mask of the descendant of the transferred handle.
  • [out] outDstHandle – pointer to the descendant of the transferred handle (from the handle space of the process that received the handle).

Returned values

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

Page top