The “clientos” library is responsible for initializing certain portions of the client operating system in an OSKit kernel. Specifically, the default memory object, the global registry, the C/POSIX library environment, and the default console, must all be initialized in order for the application to work properly. These interfaces are then available to the various OSKit libraries and components, without requiring linktime dependencies on them. The C library in particular is dependent on many external interfaces, which are accessed through its services database. That services database is given to the C library by the clientos initialization function once it has finished creating all the necessary objects. For example, before the application can call the malloc routine in the C library, the default memory object must be created (see Section 13.4), the global registry must be created (see Section 5.2), the memory object registered in the global registry, and a reference to the global registry (an instance of a services database) given to the C library so that it can request a reference to the memory object. At this point, malloc can now ask the memory object to allocate the requested amount of memory. Many other interfaces must also be installed. As the OSKit kernel continues to initialize devices and interfaces, it will hand those objects to the clientos, or in some cases the objects will be registered in the global registry directly. This approach enables the separation of the kernel intialization from the application itself.
The clientos is the first library that must be initialized in the application’s main program. Any attempts to allocate memory prior to this initialization will fail, and the kernel will most likely panic. To initialize the clientos library:
#include <oskit/clientos.h>
oskit_error_t oskit_clientos_init(void);
oskit_error_t oskit_clientos_init_pthreads(void);
Initialize the Client Operating System library. This routine must be called immediately in the application’s main program. In multi threaded applications, use the oskit_clientos_init_pthreads interface instead. As an example, consider the trivial “Hello World” program:
|
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
Several convenience functions are exported from the clientos library that make it easy to initialize the oskit_libcenv object as the application continues to initialize devices and interfaces. They are:
Set the operating system hostname. This is typically called from the network initialization code, once the hostname has been determined.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
Set the filesystem namespce object. This is typically called from the filesystem initialization code, once the root filesystem has been initialized, and the filesystem namespace object created.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
One of the many COM interfaces that are installed in the services database received by the C/POSIX library, is the oskit_libcenv COM interface. The oskit_libcenv encapsulates a number of external interfaces that only the C library needs. For example, before the application can use any of the filesystem interface calls, it must request a reference to the filesystem namespace object (see Section 23), which handles translation from multi component pathnames to oskit_file COM objects. The oskit_libcenv COM interface can be described as an ad-hoc collection of interfaces that do not belong anyplace else since only the C/POSIX libraries require them.
The oskit_libcenv COM interface inherits from IUnknown, and has the following additional methods:
In the descriptions that follow, it should be noted that the accessor function are intended to be used by the C/POSIX libraries, while the the functions to modify the object are intended to be used by the clientos library when setting up the object.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_getfsnamespace(oskit_libcenv_t *c, oskit_fsnamespace_t **out_fsn);
Get the oskit_fsnamespace COM object from the oskit_libcenv COM object. The application initialization code will typically set the oskit_fsnamespace object when it initializes the root filesystem (see oskit_clientos_setfsnamespace above). The POSIX library then requests a reference to namespace object when the application first tries to use one of the filesystem interface calls in the POSIX library.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_setfsnamespace(oskit_libcenv_t *c, oskit_fsnamespace_t *fsn);
Set the oskit_fsnamespace COM object associated with the oskit_libcenv COM object. The application initialization code will typically set the oskit_fsnamespace object when it initializes the root filesystem (see oskit_clientos_setfsnamespace above). The POSIX library then requests a reference to namespace object when the application first tries to use one of the filesystem interface calls in the POSIX library.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_gethostname(oskit_libcenv_t *c, char *hostname, int len);
Get the system hostname from the oskit_libcenv COM object. The application initialization code will typically set the hostname when it initializes the network. The POSIX library will then request the hostname as needed by the C library or the application program.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_sethostname(oskit_libcenv_t *c, char *hostname, int len);
Set the system hostname associated with the oskit_libcenv COM object. The application initialization code will typically set the hostname when it initializes the network. The POSIX library will then request the hostname as needed by the C library or the application program.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
Call the exit function for the application. This function is default to the OSKit kernel exit routine, which causes a reboot.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_setexit(oskit_libcenv_t *c, void (*exitfunc)(int));
Set the exit function for the application.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_getconsole(oskit_libcenv_t *c, oskit_ttystream_t **out_ttystream);
Get the system console object. The system console object defaults to a trivial stream implementation that uses the kernel console routines.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_setconsole(oskit_libcenv_t *c, oskit_ttystream_t *ttystream);
Set the system console object. Because the console is in use from the moment the kernel starts running, changing the console is more complicated than just using the setconsole method. The reader is encouraged to look at the example kernel in examples/x86/extended/console_tty, and the support code in startup/start_console.c.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_signals_init(oskit_libcenv_t *c, int (*func)(int, int, void *));
Call the signal initialization function for the POSIX library. The signal initialization function defaults to the OSKit kernel library signal initialization routine (see Section 15.21). The POSIX library will call this routine if the application uses any of the POSIX signal interface functions, passing in a function pointer to the callback in the POSIX library that should be invoked when a hardware trap should be passed to the application as a signal. The signal initialization function can be changed with the setsiginit method, although that should be done with caution.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_setsiginit(oskit_libcenv_t *c, void (*sigfunc)(int (*func)(int,int,void *)));
Set the signal initialization function for the POSIX library. The signal initialization function defaults to the OSKit kernel library signal initialization routine (see Section 15.21). The POSIX library will call this routine if the application uses any of the POSIX signal interface functions, passing in a function pointer to the callback in the POSIX library that should be invoked when a hardware trap should be passed to the application as a signal. Changing the signal initialization function should be done with caution.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
#include <oskit/com/libcenv.h>
void oskit_libcenv_sleep_init(oskit_libcenv_t *c, osenv_sleeprec_t *sleeprec);
The sleep/wakeup interface is provided so that the C/POSIX library uses a standard mechanism for giving up control of the CPU, in both single and multi threaded applications. The sleep_init method initializes a “sleep record” structure in preparation for the going to sleep waiting for some event to occur. The sleep record is used to avoid races between actually going to sleep and the event of interest, and to provide a “handle” on the current activity by which wakeup can indicate which process to awaken.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_sleep(oskit_libcenv_t *c, osenv_sleeprec_t *sleeprec, struct oskit_timespec *timeout);
The sleep method is used to put the caller to sleep. The specified sleep record must have been initialized with sleep_init. An optional timeout value may be supplied. The caller will be woken if the timeout expires, and OSKIT_ETIMEDOUT will be returned to indicate timeout.
The sleep function returns 0 if woken up normally, otherwise OSKIT_ETIMEDOUT is returned if the timeout expires.
#include <oskit/com/libcenv.h>
void oskit_libcenv_wakeup(oskit_libcenv_t *c, osenv_sleeprec_t *sleeprec);
The wakeup methods is used to initiate a wakeup of any thread which has previously called sleep with the indicated sleep record.
#include <oskit/com/libcenv.h>
oskit_error_t oskit_libcenv_clone(oskit_libcenv_t *c, oskit_libcenv_t **out_intf);
Make a copy of the oskit_libcenv object in c. All of the reference counts on the internal objects are adjusted, and a new oskit_libcenv object is returned in out_intf. The new object may then modified without affecting the original object.
Returns 0 on success, or an error code specified in <oskit/error.h>, on error.
The oskit_mem COM interface defines an interface for memory allocation and deallocation for oskit libraries. As described above, the C libary malloc routines are implemented in terms of an oskit_mem object that is created when the clientos is initialized. This initial memory object is the lowest level memory allocator that is available to the application. All other memory allocators, such as the malloc library, the memdebug library (see Section 31), and the device memory allocators (see Section 8), are implemented in terms of the oskit_mem object that is created when the clientos is initialized.
The oskit_mem COM interface inherits from IUnknown, and has the following additional methods:
#include <oskit/com/mem.h>
void *oskit_mem_alloc(oskit_mem_t *m, oskit_u32_t size, oskit_u32_t flags);
Allocate a chunk of memory of size bytes, subject to various options specified in flags. If successful, a pointer to the new chunk of memory is returned. Othersize a NULL pointer is returned. The new memory must be deallocated with the free method described below. The options that can be specifed with the flags parameter are:
Returns a pointer to the new chunk of memory on success, or NULL if the request could not be satisfied.
#include <oskit/com/mem.h>
void *oskit_mem_realloc(oskit_mem_t *m, void *ptr, oskit_u32_t oldsize, oskit_u32_t newsize, oskit_u32_t flags);
Change the size of the previously allocated memory chunk referenced by ptr, returning a pointer to the memory chunk. The flags must include OSKIT_MEM_AUTO_SIZE if the original allocation did, otherwise oldsize must properly give the size of the original allocation. If the new size is larger, the contents of the newly allocated portion is undefined. Any other flags specified in the original allocation should necessarily be given as well. If the size of the original chunk cannot be changed in place, a new chunk of the proper size will be allocated, and the contents of the original chunk copied to it.
Returns a pointer to the chunk of memory on success, or NULL if the request could not be satisfied.
#include <oskit/com/mem.h>
oskit_error_t oskit_mem_alloc_aligned(oskit_mem_t *m, oskit_u32_t size, oskit_u32_t flags, oskit_u32_t align);
Allocate a chunk of memory of size bytes, subject to various options specified in flags, and an alignment constraint specifed by align. The alignment constraint is a power of two integer, which indicates the minimum required alignment for the new chunk. If successful, a pointer to the new chunk of memory is returned. Othersize a NULL pointer is returned. The new memory must be deallocated with the free method described below.
Returns a pointer to the chunk of memory on success, or NULL if the request could not be satisfied.
#include <oskit/com/mem.h>
void oskit_mem_free(oskit_mem_t *m, void *ptr, oskit_u32_t size, oskit_u32_t flags);
Deallocate the chunk of memory pointed to by ptr. The flags must include OSKIT_MEM_AUTO_SIZE if the original allocation did, otherwise size must properly give the size of the original allocation.
Return the size of the chunk of memory pointed to by ptr. The chunk must have been allocated with OSKIT_MEM_AUTO_SIZE for the size to be determined. The returned size may be greater than the original size requested, because of rounding done to satisfy alignment constraints.
Returns the size of the chunk, or an undefined value if the chunk was not allocated with OSKIT_MEM_AUTO_SIZE.
#include <oskit/com/mem.h>
void *oskit_mem_alloc_gen(oskit_mem_t *m, oskit_u32_t size, oskit_u32_t flags, oskit_u32_t align_bits, oskit_u32_t align_ofs);
Allocate a chunk of memory meeting various alignment and address constraints. It is similar to alloc, but is intended to provide an interface more like lmm_alloc_gen (see Section 25).
Returns a pointer to the chunk of memory on success, or NULL if the request could not be satisfied.
Return the amount of free space in the memory object pool. If flags is non-zero, it should be a memory type flag, which indicates that the return value should be the amount of free space of that type.
Returns the amount of memory available on success.