2013-09-06 17:37:09 +02:00
|
|
|
/*
|
|
|
|
* \brief Kernel backend for protection domains
|
|
|
|
* \author Martin Stein
|
2014-04-28 21:31:57 +02:00
|
|
|
* \author Stefan Kalkowski
|
2013-09-06 17:37:09 +02:00
|
|
|
* \date 2012-11-30
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2012-2013 Genode Labs GmbH
|
|
|
|
*
|
|
|
|
* This file is part of the Genode OS framework, which is distributed
|
|
|
|
* under the terms of the GNU General Public License version 2.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _KERNEL__PD_H_
|
|
|
|
#define _KERNEL__PD_H_
|
|
|
|
|
2013-12-17 18:10:02 +01:00
|
|
|
/* Genode includes */
|
|
|
|
#include <cpu/atomic.h>
|
2014-11-14 10:44:56 +01:00
|
|
|
#include <cpu/memory_barrier.h>
|
2013-12-17 18:10:02 +01:00
|
|
|
|
2013-09-06 17:37:09 +02:00
|
|
|
/* core includes */
|
2014-07-28 16:55:47 +02:00
|
|
|
#include <kernel/early_translations.h>
|
2013-09-06 17:37:09 +02:00
|
|
|
#include <kernel/configuration.h>
|
|
|
|
#include <kernel/object.h>
|
2014-10-10 16:13:52 +02:00
|
|
|
#include <kernel/cpu.h>
|
2013-09-06 17:37:09 +02:00
|
|
|
#include <assert.h>
|
2015-02-19 14:50:27 +01:00
|
|
|
#include <page_slab.h>
|
|
|
|
#include <board.h>
|
2013-09-06 17:37:09 +02:00
|
|
|
|
|
|
|
/* structure of the mode transition */
|
|
|
|
extern int _mt_begin;
|
|
|
|
extern int _mt_end;
|
|
|
|
extern int _mt_user_entry_pic;
|
|
|
|
extern Genode::addr_t _mt_client_context_ptr;
|
|
|
|
extern Genode::addr_t _mt_master_context_begin;
|
|
|
|
extern Genode::addr_t _mt_master_context_end;
|
|
|
|
|
2013-12-17 18:10:02 +01:00
|
|
|
namespace Kernel
|
|
|
|
{
|
|
|
|
/**
|
2014-07-09 12:03:17 +02:00
|
|
|
* Lock that enables synchronization inside the kernel
|
2013-12-17 18:10:02 +01:00
|
|
|
*/
|
|
|
|
class Lock;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Kernel::Lock
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
int volatile _locked;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
Lock() : _locked(0) { }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Request the lock
|
|
|
|
*/
|
|
|
|
void lock() { while (!Genode::cmpxchg(&_locked, 0, 1)); }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Free the lock
|
|
|
|
*/
|
|
|
|
void unlock()
|
|
|
|
{
|
2014-11-14 10:44:56 +01:00
|
|
|
Genode::memory_barrier();
|
2013-12-17 18:10:02 +01:00
|
|
|
_locked = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provide guard semantic for this type of lock
|
|
|
|
*/
|
|
|
|
typedef Genode::Lock_guard<Kernel::Lock> Guard;
|
|
|
|
};
|
|
|
|
|
2013-09-06 17:37:09 +02:00
|
|
|
namespace Kernel
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Controls the mode-transition page
|
|
|
|
*
|
|
|
|
* The mode transition page is a small memory region that is mapped by
|
|
|
|
* every PD to the same virtual address. It contains code that acts as a
|
|
|
|
* link between high privileged CPU mode (often called kernel) and low
|
|
|
|
* privileged CPU mode (often called userland). The mode transition
|
|
|
|
* control provides a simple interface to access the code from within
|
|
|
|
* the kernel.
|
|
|
|
*/
|
2014-07-28 16:55:47 +02:00
|
|
|
class Mode_transition_control;
|
2013-09-06 17:37:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the system wide mode-transition control
|
|
|
|
*/
|
2013-10-16 12:30:10 +02:00
|
|
|
Mode_transition_control * mtc();
|
2013-09-06 17:37:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Kernel backend of protection domains
|
|
|
|
*/
|
|
|
|
class Pd;
|
2013-10-07 14:56:31 +02:00
|
|
|
|
2013-10-11 12:29:40 +02:00
|
|
|
class Pd_ids : public Id_allocator<MAX_PDS> { };
|
|
|
|
typedef Object_pool<Pd> Pd_pool;
|
2013-10-07 14:56:31 +02:00
|
|
|
|
|
|
|
Pd_ids * pd_ids();
|
|
|
|
Pd_pool * pd_pool();
|
2013-12-17 18:10:02 +01:00
|
|
|
|
|
|
|
Lock & data_lock();
|
2013-09-06 17:37:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class Kernel::Mode_transition_control
|
|
|
|
{
|
|
|
|
friend class Pd;
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
2014-07-28 16:55:47 +02:00
|
|
|
typedef Early_translations_allocator Allocator;
|
|
|
|
typedef Early_translations_slab Slab;
|
|
|
|
typedef Genode::Translation_table Table;
|
|
|
|
typedef Genode::Page_flags Page_flags;
|
2013-09-06 17:37:09 +02:00
|
|
|
|
2014-07-28 16:55:47 +02:00
|
|
|
Allocator _allocator;
|
|
|
|
Slab _slab;
|
|
|
|
Table _table;
|
|
|
|
Cpu_context _master;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return size of the mode transition
|
|
|
|
*/
|
|
|
|
static size_t _size() { return (addr_t)&_mt_end - (addr_t)&_mt_begin; }
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return size of master-context space in the mode transition
|
|
|
|
*/
|
|
|
|
static size_t _master_context_size()
|
|
|
|
{
|
|
|
|
addr_t const begin = (addr_t)&_mt_master_context_begin;
|
|
|
|
addr_t const end = (addr_t)&_mt_master_context_end;
|
|
|
|
return end - begin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return virtual address of the user entry-code
|
|
|
|
*/
|
|
|
|
static addr_t _virt_user_entry()
|
|
|
|
{
|
|
|
|
addr_t const phys = (addr_t)&_mt_user_entry_pic;
|
|
|
|
addr_t const phys_base = (addr_t)&_mt_begin;
|
|
|
|
return VIRT_BASE + (phys - phys_base);
|
|
|
|
}
|
2013-09-06 17:37:09 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
enum {
|
2014-07-28 16:55:47 +02:00
|
|
|
SIZE_LOG2 = Genode::Translation_table::MIN_PAGE_SIZE_LOG2,
|
|
|
|
SIZE = 1 << SIZE_LOG2,
|
2014-10-10 16:13:52 +02:00
|
|
|
VIRT_BASE = Cpu::exception_entry,
|
2014-07-28 16:55:47 +02:00
|
|
|
ALIGN_LOG2 = Genode::Translation_table::ALIGNM_LOG2,
|
|
|
|
ALIGN = 1 << ALIGN_LOG2,
|
2013-09-06 17:37:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* \param c CPU context for kernel mode entry
|
|
|
|
*/
|
2014-07-28 16:55:47 +02:00
|
|
|
Mode_transition_control();
|
2013-09-06 17:37:09 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Map the mode transition page to a virtual address space
|
|
|
|
*
|
2014-04-28 21:31:57 +02:00
|
|
|
* \param tt translation buffer of the address space
|
2013-09-06 17:37:09 +02:00
|
|
|
* \param ram RAM donation for mapping (first try without)
|
|
|
|
*/
|
2014-04-28 21:31:57 +02:00
|
|
|
void map(Genode::Translation_table * tt,
|
|
|
|
Genode::Page_slab * alloc)
|
2013-09-06 17:37:09 +02:00
|
|
|
{
|
2014-04-28 21:31:57 +02:00
|
|
|
try {
|
|
|
|
addr_t const phys_base = (addr_t)&_mt_begin;
|
|
|
|
tt->insert_translation(VIRT_BASE, phys_base, SIZE,
|
|
|
|
Page_flags::mode_transition(), alloc);
|
|
|
|
} catch(...) {
|
|
|
|
PERR("Inserting exception vector in page table failed!"); }
|
2013-09-06 17:37:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-02-19 14:50:27 +01:00
|
|
|
* Continue execution of client context
|
|
|
|
*
|
|
|
|
* \param context targeted CPU context
|
|
|
|
* \param cpu kernel name of targeted CPU
|
|
|
|
* \param entry_raw raw pointer to assembly entry-code
|
|
|
|
* \param context_ptr_base base address of client-context pointer region
|
2013-09-06 17:37:09 +02:00
|
|
|
*/
|
2015-02-19 14:50:27 +01:00
|
|
|
void switch_to(Cpu::Context * const context,
|
|
|
|
unsigned const cpu,
|
|
|
|
addr_t const entry_raw,
|
|
|
|
addr_t const context_ptr_base)
|
|
|
|
{
|
|
|
|
/* override client-context pointer of the executing CPU */
|
|
|
|
size_t const context_ptr_offset = cpu * sizeof(context);
|
|
|
|
addr_t const context_ptr = context_ptr_base + context_ptr_offset;
|
|
|
|
*(void * *)context_ptr = context;
|
|
|
|
|
|
|
|
/* unlock kernel data */
|
|
|
|
data_lock().unlock();
|
|
|
|
|
|
|
|
/* call assembly code that applies the virtual-machine context */
|
|
|
|
typedef void (* Entry)();
|
|
|
|
Entry __attribute__((noreturn)) const entry = (Entry)entry_raw;
|
|
|
|
entry();
|
|
|
|
}
|
2013-09-06 17:37:09 +02:00
|
|
|
|
|
|
|
/**
|
2015-02-19 14:50:27 +01:00
|
|
|
* Continue execution of user context
|
|
|
|
*
|
|
|
|
* \param context targeted CPU context
|
|
|
|
* \param cpu kernel name of targeted CPU
|
2013-09-06 17:37:09 +02:00
|
|
|
*/
|
2015-02-19 14:50:27 +01:00
|
|
|
void switch_to_user(Cpu::Context * const context,
|
|
|
|
unsigned const cpu)
|
|
|
|
{
|
|
|
|
switch_to(context, cpu, _virt_user_entry(),
|
|
|
|
(addr_t)&_mt_client_context_ptr);
|
|
|
|
}
|
2014-07-28 16:55:47 +02:00
|
|
|
} __attribute__((aligned(Mode_transition_control::ALIGN)));
|
2013-09-06 17:37:09 +02:00
|
|
|
|
2013-10-11 12:29:40 +02:00
|
|
|
class Kernel::Pd : public Object<Pd, MAX_PDS, Pd_ids, pd_ids, pd_pool>
|
2013-09-06 17:37:09 +02:00
|
|
|
{
|
2014-07-28 16:55:47 +02:00
|
|
|
public:
|
|
|
|
|
|
|
|
typedef Genode::Translation_table Table;
|
|
|
|
|
2013-09-06 17:37:09 +02:00
|
|
|
private:
|
|
|
|
|
2014-07-28 16:55:47 +02:00
|
|
|
Table * const _table;
|
|
|
|
Platform_pd * const _platform_pd;
|
2013-09-06 17:37:09 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
2014-07-28 16:55:47 +02:00
|
|
|
* \param table translation table of the PD
|
2013-09-06 17:37:09 +02:00
|
|
|
* \param platform_pd core object of the PD
|
|
|
|
*/
|
2014-07-28 16:55:47 +02:00
|
|
|
Pd(Table * const table, Platform_pd * const platform_pd)
|
|
|
|
: _table(table), _platform_pd(platform_pd) { }
|
2013-09-18 13:12:32 +02:00
|
|
|
|
2013-09-06 17:37:09 +02:00
|
|
|
/**
|
|
|
|
* Let the CPU context 'c' join the PD
|
|
|
|
*/
|
2014-10-10 16:13:52 +02:00
|
|
|
void admit(Cpu::Context * const c)
|
2013-09-06 17:37:09 +02:00
|
|
|
{
|
|
|
|
c->protection_domain(id());
|
2014-04-28 21:31:57 +02:00
|
|
|
c->translation_table((addr_t)translation_table());
|
2013-09-06 17:37:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/***************
|
|
|
|
** Accessors **
|
|
|
|
***************/
|
|
|
|
|
|
|
|
Platform_pd * platform_pd() const { return _platform_pd; }
|
2014-07-28 16:55:47 +02:00
|
|
|
Table * translation_table() const { return _table; }
|
2013-09-06 17:37:09 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* _KERNEL__PD_H_ */
|