mirror of
https://github.com/mmueller41/genode.git
synced 2026-01-21 12:32:56 +01:00
This patch extends the RAM session interface with the ability to allocate DMA buffers. The client specifies the type of RAM dataspace to allocate via the new 'cached' argument of the 'Ram_session::alloc()' function. By default, 'cached' is true, which correponds to the common case and the original behavior. When setting 'cached' to 'false', core takes the precautions needed to register the memory as uncached in the page table of each process that has the dataspace attached. Currently, the support for allocating DMA buffers is implemented for Fiasco.OC only. On x86 platforms, it is generally not needed. But on platforms with more relaxed cache coherence (such as ARM), user-level device drivers should always use uncacheable memory for DMA transactions.
96 lines
2.1 KiB
C++
96 lines
2.1 KiB
C++
/*
|
|
* \brief A guard for RAM session clients to limit memory exhaustion
|
|
* \author Christian Prochaska
|
|
* \date 2012-04-25
|
|
*/
|
|
|
|
/*
|
|
* Copyright (C) 2012 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 _RAM_SESSION_CLIENT_GUARD_H_
|
|
#define _RAM_SESSION_CLIENT_GUARD_H_
|
|
|
|
#include <base/lock.h>
|
|
#include <base/printf.h>
|
|
#include <dataspace/client.h>
|
|
#include <ram_session/client.h>
|
|
|
|
namespace Genode {
|
|
|
|
class Ram_session_client_guard : public Ram_session_client
|
|
{
|
|
private:
|
|
|
|
size_t _amount; /* total amount */
|
|
size_t _consumed; /* already consumed bytes */
|
|
Lock _consumed_lock;
|
|
|
|
public:
|
|
|
|
Ram_session_client_guard(Ram_session_capability session, size_t amount)
|
|
: Ram_session_client(session), _amount(amount), _consumed(0) { }
|
|
|
|
Ram_dataspace_capability alloc(size_t size, bool cached)
|
|
{
|
|
Lock::Guard _consumed_lock_guard(_consumed_lock);
|
|
|
|
if ((_amount - _consumed) < size) {
|
|
PWRN("Quota exceeded! amount=%zu, size=%zu, consumed=%zu",
|
|
_amount, size, _consumed);
|
|
return Ram_dataspace_capability();
|
|
}
|
|
|
|
Ram_dataspace_capability cap =
|
|
Ram_session_client::alloc(size, cached);
|
|
|
|
_consumed += size;
|
|
|
|
return cap;
|
|
}
|
|
|
|
void free(Ram_dataspace_capability ds)
|
|
{
|
|
Lock::Guard _consumed_lock_guard(_consumed_lock);
|
|
|
|
_consumed -= Dataspace_client(ds).size();
|
|
|
|
Ram_session_client::free(ds);
|
|
}
|
|
|
|
int transfer_quota(Ram_session_capability ram_session, size_t amount)
|
|
{
|
|
Lock::Guard _consumed_lock_guard(_consumed_lock);
|
|
|
|
if ((_amount - _consumed) < amount) {
|
|
PWRN("Quota exceeded! amount=%zu, size=%zu, consumed=%zu",
|
|
_amount, amount, _consumed);
|
|
return -1;
|
|
}
|
|
|
|
int result = Ram_session_client::transfer_quota(ram_session, amount);
|
|
|
|
if (result == 0)
|
|
_consumed += amount;
|
|
|
|
return result;
|
|
}
|
|
|
|
size_t quota()
|
|
{
|
|
return _amount;
|
|
}
|
|
|
|
size_t used()
|
|
{
|
|
Lock::Guard _consumed_lock_guard(_consumed_lock);
|
|
return _consumed;
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif /* _RAM_SESSION_CLIENT_GUARD_H_ */
|