os: introduce C-API to Genode services

This commit introduces a C-API to the Uplink session, as well as to
serve as a Block service. It can be used by drivers ported from
C-only projects, like the Linux kernel, or BSD kernels for instance.

Fix #4226
This commit is contained in:
Stefan Kalkowski
2021-07-20 11:38:27 +02:00
committed by Christian Helmuth
parent 1a526e73a3
commit ee045a68cc
7 changed files with 937 additions and 0 deletions

View File

@ -0,0 +1,79 @@
/*
* \brief C interface to Genode's base types
* \author Norman Feske
* \date 2021-07-06
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _INCLUDE__GENODE_C_API__BASE_H_
#define _INCLUDE__GENODE_C_API__BASE_H_
#ifdef __cplusplus
extern "C" {
#endif
/**********************************************
** Forward-declared types used in the C API **
**********************************************/
struct genode_env;
struct genode_allocator;
struct genode_signal_handler;
struct genode_attached_dataspace;
#ifdef __cplusplus
} /* extern "C" */
#endif
/**********************************************************************
** Mapping between C types and their corresponding Genode C++ types **
**********************************************************************/
#ifdef __cplusplus
#include <base/env.h>
#include <base/allocator.h>
#include <base/attached_dataspace.h>
struct genode_env : Genode::Env { };
struct genode_allocator : Genode::Allocator { };
struct genode_signal_handler : Genode::Signal_dispatcher_base { };
struct genode_attached_dataspace : Genode::Attached_dataspace { };
static inline auto genode_env_ptr(Genode::Env &env)
{
return static_cast<genode_env *>(&env);
}
static inline auto genode_allocator_ptr(Genode::Allocator &alloc)
{
return static_cast<genode_allocator *>(&alloc);
}
static inline auto genode_signal_handler_ptr(Genode::Signal_dispatcher_base &sigh)
{
return static_cast<genode_signal_handler *>(&sigh);
}
static inline Genode::Signal_context_capability cap(genode_signal_handler *sigh_ptr)
{
Genode::Signal_dispatcher_base *dispatcher_ptr = sigh_ptr;
return *static_cast<Genode::Signal_handler<Genode::Meta::Empty> *>(dispatcher_ptr);
}
static inline auto genode_attached_dataspace_ptr(Genode::Attached_dataspace &ds)
{
return static_cast<genode_attached_dataspace *>(&ds);
}
#endif
#endif /* _INCLUDE__GENODE_C_API__BASE_H_ */

View File

@ -0,0 +1,102 @@
/*
* \brief C-API Genode block backend
* \author Stefan Kalkowski
* \date 2021-07-05
*/
/*
* Copyright (C) 2006-2021 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _GENODE_C_API__BLOCK_H_
#define _GENODE_C_API__BLOCK_H_
#include <genode_c_api/base.h>
struct genode_block_session; /* definition is private to the implementation */
#ifdef __cplusplus
extern "C" {
#endif
/********************
** Initialization **
********************/
/**
* Callback called during peer session request to allocate dma-capable shared buffer
*/
typedef struct genode_attached_dataspace * (*genode_block_alloc_peer_buffer_t)
(unsigned long size);
/**
* Callback called when closing peer session to free shared buffer
*/
typedef void (*genode_block_free_peer_buffer_t)
(struct genode_attached_dataspace * ds);
/**
* Initialize block root component
*
* \param handler signal handler to be installed at each block session
*/
void genode_block_init(struct genode_env *env,
struct genode_allocator *alloc,
struct genode_signal_handler *handler,
genode_block_alloc_peer_buffer_t,
genode_block_free_peer_buffer_t);
/**************************************
** Block device lifetime management **
**************************************/
typedef unsigned long long genode_block_sector_t;
void genode_block_announce_device(const char * name,
genode_block_sector_t sectors,
int writeable);
void genode_block_discontinue_device(const char * name);
/************************************
** Block session request handling **
************************************/
enum Operation {
GENODE_BLOCK_READ,
GENODE_BLOCK_WRITE,
GENODE_BLOCK_SYNC,
GENODE_BLOCK_UNAVAIL,
};
struct genode_block_request {
int op;
genode_block_sector_t blk_nr;
genode_block_sector_t blk_cnt;
void * addr;
};
struct genode_block_session * genode_block_session_by_name(const char * name);
struct genode_block_request *
genode_block_request_by_session(struct genode_block_session * const session);
void genode_block_ack_request(struct genode_block_session * const session,
struct genode_block_request * const request,
int success);
void genode_block_notify_peers(void);
#ifdef __cplusplus
}
#endif
#endif /* _GENODE_C_API__BLOCK_H_ */

View File

@ -0,0 +1,107 @@
/*
* \brief C interface to Genode's uplink session
* \author Norman Feske
* \date 2021-07-06
*/
/*
* Copyright (C) 2006-2017 Genode Labs GmbH
*
* This file is part of the Genode OS framework, which is distributed
* under the terms of the GNU Affero General Public License version 3.
*/
#ifndef _INCLUDE__GENODE_C_API__UPLINK_H_
#define _INCLUDE__GENODE_C_API__UPLINK_H_
#include <genode_c_api/base.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* Initialize uplink handling
*
* \param sigh signal handler to be installed at the uplink connection
*/
void genode_uplink_init(struct genode_env *,
struct genode_allocator *,
struct genode_signal_handler *);
/**
* Wake up uplink server if progress can be made at the server side
*
* This function should be called whenever the component becomes idle.
*/
void genode_uplink_notify_peers(void);
struct genode_uplink; /* definition is private to the implementation */
/*********************
** Uplink lifetime **
*********************/
struct genode_uplink_args
{
unsigned char mac_address[6];
char const *label;
};
struct genode_uplink *genode_uplink_create(struct genode_uplink_args const *);
void genode_uplink_destroy(struct genode_uplink *);
/*************************************************
** Transmit packets towards the uplink session **
*************************************************/
struct genode_uplink_tx_packet_context;
/**
* Callback called by 'genode_uplink_tx_packet' to provide the content
*/
typedef unsigned long (*genode_uplink_tx_packet_content_t)
(struct genode_uplink_tx_packet_context *, char *dst, unsigned long dst_len);
/**
* Process packet transmission
*
* \return true if progress was made
*/
bool genode_uplink_tx_packet(struct genode_uplink *,
genode_uplink_tx_packet_content_t,
struct genode_uplink_tx_packet_context *);
/*********************************************
** Receive packets from the uplink session **
*********************************************/
struct genode_uplink_rx_context;
typedef enum { GENODE_UPLINK_RX_REJECTED,
GENODE_UPLINK_RX_ACCEPTED,
GENODE_UPLINK_RX_RETRY } genode_uplink_rx_result_t;
typedef genode_uplink_rx_result_t (*genode_uplink_rx_one_packet_t)
(struct genode_uplink_rx_context *, char const *ptr, unsigned long len);
/**
* Process packet reception
*
* \return true if progress was made
*/
bool genode_uplink_rx(struct genode_uplink *,
genode_uplink_rx_one_packet_t rx_one_packet,
struct genode_uplink_rx_context *);
#ifdef __cplusplus
}
#endif
#endif /* _INCLUDE__GENODE_C_API__UPLINK_H_ */