11#include "vcon_stream.h"
14#include <l4/bid_config.h>
19#include <l4/cxx/hlist>
21#include <l4/cxx/std_alloc>
23#include <l4/l4re_vfs/backend>
33static int debug_mmap = 1;
34#define DEBUG_LOG(level, dbg...) do { if (level) dbg } while (0)
36#define DEBUG_LOG(level, dbg...) do { } while (0)
44#define USE_BIG_ANON_DS
52class Fd_store :
public L4Re::Core::Fd_store
60class Std_stream : public
L4Re::Core::Vcon_stream
66Fd_store::Fd_store() noexcept
70 static char m[
sizeof(Std_stream)] __attribute__((aligned(
sizeof(
long))));
74 set(0, cxx::ref_ptr(s));
75 set(1, cxx::ref_ptr(s));
76 set(2, cxx::ref_ptr(s));
79class Root_mount_tree :
public L4Re::Vfs::Mount_tree
82 Root_mount_tree() :
L4Re::Vfs::Mount_tree(0) {}
83 void operator delete (
void *) {}
93 : _early_oom(true), _root_mount(), _root(
L4Re::Env::env())
95 _root_mount.add_ref();
97 _root_mount.mount(cxx::ref_ptr(&_root));
98 _cwd = cxx::ref_ptr(&_root);
101 Ref_ptr<L4Re::Vfs::File> rom;
102 _root.openat(
"rom", 0, 0, &rom);
104 _root_mount.create_tree(
"lib/foo", rom);
106 _root.openat(
"lib", 0, 0, &_cwd);
111 int alloc_fd(Ref_ptr<L4Re::Vfs::File>
const &f)
noexcept override;
112 Ref_ptr<L4Re::Vfs::File> free_fd(
int fd)
noexcept override;
113 Ref_ptr<L4Re::Vfs::File> get_root() noexcept override;
114 Ref_ptr<
L4Re::Vfs::File> get_cwd() noexcept override;
115 void set_cwd(Ref_ptr<
L4Re::Vfs::File> const &dir) noexcept override;
116 Ref_ptr<
L4Re::Vfs::File> get_file(
int fd) noexcept override;
117 cxx::Pair<Ref_ptr<
L4Re::Vfs::File>,
int>
118 set_fd(
int fd, Ref_ptr<
L4Re::Vfs::File> const &f = Ref_ptr<>::Nil) noexcept
121 int mmap2(
void *start,
size_t len,
int prot,
int flags,
int fd,
122 off_t offset,
void **ptr) noexcept override;
124 int munmap(
void *start,
size_t len) noexcept override;
125 int mremap(
void *old,
size_t old_sz,
size_t new_sz,
int flags,
126 void **new_addr) noexcept override;
127 int mprotect(const
void *a,
size_t sz,
int prot) noexcept override;
128 int msync(
void *addr,
size_t len,
int flags) noexcept override;
129 int madvise(
void *addr,
size_t len,
int advice) noexcept override;
131 int register_file_system(
L4Re::Vfs::File_system *f) noexcept override;
132 int unregister_file_system(
L4Re::Vfs::File_system *f) noexcept override;
133 L4Re::Vfs::File_system *get_file_system(
char const *fstype) noexcept override;
134 L4Re::Vfs::File_system_list file_system_list() noexcept override;
136 int register_file_factory(
cxx::Ref_ptr<
L4Re::Vfs::File_factory> f) noexcept override;
137 int unregister_file_factory(
cxx::Ref_ptr<
L4Re::Vfs::File_factory> f) noexcept override;
138 Ref_ptr<
L4Re::Vfs::File_factory> get_file_factory(
int proto) noexcept override;
139 Ref_ptr<
L4Re::Vfs::File_factory> get_file_factory(
char const *proto_name) noexcept override;
140 int mount(
char const *path,
cxx::Ref_ptr<
L4Re::Vfs::File> const &dir) noexcept override;
142 void operator delete (
void *) {}
144 void *malloc(
size_t size)
noexcept override {
return Vfs_config::malloc(size); }
145 void free(
void *m)
noexcept override { Vfs_config::free(m); }
148 Root_mount_tree _root_mount;
149 L4Re::Core::Env_dir _root;
150 Ref_ptr<L4Re::Vfs::File> _cwd;
161 File_factory_item() =
default;
162 File_factory_item(File_factory_item
const &) =
delete;
163 File_factory_item &operator = (File_factory_item
const &) =
delete;
175 void align_mmap_start_and_length(
void **start,
size_t *length);
176 int munmap_regions(
void *start,
size_t len);
181static inline bool strequal(
char const *a,
char const *b)
183 for (;*a && *a == *b; ++a, ++b)
196 for (File_system *c = _fs_registry; c; c = c->next())
197 if (strequal(c->type(), f->type()))
200 f->next(_fs_registry);
214 File_system **p = &_fs_registry;
216 for (; *p; p = &(*p)->next())
228Vfs::find_fs_from_type(
char const *fstype)
noexcept
230 L4Re::Vfs::File_system_list fsl(_fs_registry);
231 for (L4Re::Vfs::File_system_list::Iterator c = fsl.begin();
233 if (strequal(c->type(), fstype))
238L4Re::Vfs::File_system_list
239Vfs::file_system_list() noexcept
241 return L4Re::Vfs::File_system_list(_fs_registry);
245Vfs::get_file_system(
char const *fstype)
noexcept
248 if ((fs = find_fs_from_type(fstype)))
252 int res = Vfs_config::load_module(fstype);
257 return find_fs_from_type(fstype);
266 void *x = this->malloc(
sizeof(File_factory_item));
271 _file_factories.push_front(ff);
278 for (
auto p: _file_factories)
282 _file_factories.remove(p);
283 p->~File_factory_item();
291Ref_ptr<L4Re::Vfs::File_factory>
292Vfs::get_file_factory(
int proto)
noexcept
294 for (
auto p: _file_factories)
295 if (p->f->proto() == proto)
298 return Ref_ptr<L4Re::Vfs::File_factory>();
301Ref_ptr<L4Re::Vfs::File_factory>
302Vfs::get_file_factory(
char const *proto_name)
noexcept
304 for (
auto p: _file_factories)
306 auto n = p->f->proto_name();
310 char const *b = proto_name;
311 for (; *a && *b && *a == *b; ++a, ++b)
314 if ((*a == 0) && (*b == 0))
319 return Ref_ptr<L4Re::Vfs::File_factory>();
323Vfs::alloc_fd(Ref_ptr<L4Re::Vfs::File>
const &f)
noexcept
325 int fd = fds.alloc();
335Ref_ptr<L4Re::Vfs::File>
336Vfs::free_fd(
int fd)
noexcept
338 Ref_ptr<L4Re::Vfs::File> f = fds.get(fd);
341 return Ref_ptr<>::Nil;
348Ref_ptr<L4Re::Vfs::File>
349Vfs::get_root() noexcept
351 return cxx::ref_ptr(&_root);
354Ref_ptr<L4Re::Vfs::File>
355Vfs::get_cwd() noexcept
361Vfs::set_cwd(Ref_ptr<L4Re::Vfs::File>
const &dir)
noexcept
368Ref_ptr<L4Re::Vfs::File>
369Vfs::get_file(
int fd)
noexcept
375Vfs::set_fd(
int fd, Ref_ptr<L4Re::Vfs::File>
const &f)
noexcept
377 if (!fds.check_fd(fd))
378 return cxx::pair(Ref_ptr<L4Re::Vfs::File>(Ref_ptr<>::Nil), EBADF);
380 Ref_ptr<L4Re::Vfs::File> old = fds.get(fd);
382 return cxx::pair(old, 0);
386#define GET_FILE_DBG(fd, err) \
387 Ref_ptr<L4Re::Vfs::File> fi = fds.get(fd); \
393#define GET_FILE(fd, err) \
394 Ref_ptr<L4Re::Vfs::File> fi = fds.get(fd); \
399Vfs::align_mmap_start_and_length(
void **start,
size_t *length)
409Vfs::munmap_regions(
void *start,
size_t len)
412 using namespace L4Re;
421 align_mmap_start_and_length(&start, &len);
425 DEBUG_LOG(debug_mmap, {
432 err = r->detach(
l4_addr_t(start), len, &ds, This_task);
436 switch (err & Rm::Detach_result_mask)
440 L4Re::virt_cap_alloc->take(ds);
442 case Rm::Detached_ds:
444 L4Re::virt_cap_alloc->release(ds);
450 if (!(err & Rm::Detach_again))
456Vfs::munmap(
void *start,
size_t len)
L4_NOTHROW
459 using namespace L4Re;
467 bool matches_area =
false;
473 area_cnt = r->get_areas((
l4_addr_t) start, &area_array);
481 size_t area_size = area_array[0].
end - area_array[0].
start + 1;
484 if (area_array[0].start == (
l4_addr_t) start && area_size == len)
494 err = munmap_regions(start, len);
495 if (err == -ENOENT && matches_area)
504 *ds = L4Re::make_shared_cap<L4Re::Dataspace>(L4Re::virt_cap_alloc);
510 if ((err = Vfs_config::allocator()->alloc(size, ds->get())) < 0)
513 DEBUG_LOG(debug_mmap, {
528#if !defined(CONFIG_MMU)
533 ANON_MEM_DS_POOL_SIZE = 256UL << 10,
534 ANON_MEM_MAX_SIZE = 32UL << 10,
536#elif defined(USE_BIG_ANON_DS)
539 ANON_MEM_DS_POOL_SIZE = 256UL << 20,
540 ANON_MEM_MAX_SIZE = 32UL << 20,
545 ANON_MEM_DS_POOL_SIZE = 256UL << 20,
546 ANON_MEM_MAX_SIZE = 0UL << 20,
550 if (size >= ANON_MEM_MAX_SIZE)
553 if ((err = alloc_ds(size, ds)) < 0)
561 return (*ds)->allocate(0, size);
564 if (!_anon_ds.is_valid() || _anon_offset + size >= ANON_MEM_DS_POOL_SIZE)
567 if ((err = alloc_ds(ANON_MEM_DS_POOL_SIZE, ds)) < 0)
578 if (
int err = (*ds)->allocate(_anon_offset, size))
582 *offset = _anon_offset;
583 _anon_offset += size;
588Vfs::mmap2(
void *start,
size_t len,
int prot,
int flags,
int fd, off_t page4k_offset,
591 DEBUG_LOG(debug_mmap, {
606 using namespace L4Re;
609 if (flags & MAP_FIXED)
613 align_mmap_start_and_length(&start, &len);
618 if ((flags & 0x1000000) || (prot == PROT_NONE))
627 *resptr =
reinterpret_cast<void*
>(area);
629 DEBUG_LOG(debug_mmap, {
642 L4Re::Rm::Flags rm_flags(0);
644 if (flags & (MAP_ANONYMOUS | MAP_PRIVATE))
648 int err = alloc_anon_mem(len, &ds, &anon_offset);
652 DEBUG_LOG(debug_mmap, {
661 if (!(flags & MAP_ANONYMOUS))
663 Ref_ptr<L4Re::Vfs::File> fi = fds.get(fd);
675 if (flags & MAP_PRIVATE)
677 DEBUG_LOG(debug_mmap,
outstring(
"COW\n"););
678 int err = ds->copy_in(anon_offset, fds, offset, len);
684 err = r->attach(&src, len,
690 err = r->attach(&dst, len,
692 ds.get(), anon_offset);
696 memcpy(dst.
get(), src.
get(), len);
701 offset = anon_offset;
705 L4Re::virt_cap_alloc->take(fds);
710 offset = anon_offset;
713 if (!(flags & MAP_FIXED) && start == 0)
716 char *data =
static_cast<char *
>(start);
721 if (flags & MAP_FIXED)
725 err = r->reserve_area(&overmap_area, len);
729 rm_flags |= Rm::F::In_area;
734 err = munmap_regions(start, len);
735 if (err && err != -ENOENT)
739 if (!(flags & MAP_FIXED))
740 rm_flags |= Rm::F::Search_addr;
741 if (prot & PROT_READ)
742 rm_flags |= Rm::F::R;
743 if (prot & PROT_WRITE)
744 rm_flags |= Rm::F::W;
745 if (prot & PROT_EXEC)
746 rm_flags |= Rm::F::X;
748 err = r->attach(&data, len, rm_flags,
754 DEBUG_LOG(debug_mmap, {
770 r->free_area(overmap_area);
798 int e = r->reserve_area(&a, sz, flags);
815 ~Auto_area() { free(); }
820Vfs::mremap(
void *old_addr,
size_t old_size,
size_t new_size,
int flags,
823 using namespace L4Re;
825 DEBUG_LOG(debug_mmap, {
835 if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
839 if (oa !=
reinterpret_cast<l4_addr_t>(old_addr))
842 bool const fixed = flags & MREMAP_FIXED;
843 bool const maymove = flags & MREMAP_MAYMOVE;
853 if (new_size < old_size)
855 *new_addr = old_addr;
856 return munmap(
reinterpret_cast<void*
>(oa + new_size),
857 old_size - new_size);
860 if (new_size == old_size)
862 *new_addr = old_addr;
867 Auto_area old_area(r);
868 int err = old_area.reserve(oa, old_size, L4Re::Rm::Flags(0));
873 Auto_area new_area(r);
877 if (na !=
reinterpret_cast<l4_addr_t>(*new_addr))
881 int err = new_area.reserve(na, new_size, L4Re::Rm::Flags(0));
891 unsigned long ts = new_size - old_size;
893 long err = new_area.reserve(ta, ts, L4Re::Rm::Flags(0));
897 L4Re::Rm::Offset toffs;
898 L4Re::Rm::Flags tflags;
901 err = r->find(&ta, &ts, &toffs, &tflags, &tds);
904 if (err == -ENOENT || (err == 0 && (tflags & Rm::F::In_area)))
907 pad_addr = oa + old_size;
908 *new_addr = old_addr;
915 err = new_area.reserve(0, new_size, Rm::F::Search_addr);
919 pad_addr = new_area.a + old_size;
920 *new_addr =
reinterpret_cast<void *
>(new_area.a);
924 if (old_area.is_valid())
926 unsigned long size = old_size;
934 while (r->find(&a, &s, &o, &f, &ds) >= 0 && !(f & Rm::F::In_area))
938 auto d = old_area.a - a;
944 if (a + s > old_area.a + old_size)
945 s = old_area.a + old_size - a;
947 l4_addr_t x = a - old_area.a + new_area.a;
949 int err = r->attach(&x, s, Rm::F::In_area | f,
955 L4Re::virt_cap_alloc->take(ds);
957 err = r->detach(a, s, &ds, This_task,
958 Rm::Detach_exact | Rm::Detach_keep);
962 switch (err & Rm::Detach_result_mask)
967 L4Re::virt_cap_alloc->take(ds);
969 case Rm::Detached_ds:
971 L4Re::virt_cap_alloc->release(ds);
987 if (old_size < new_size)
989 l4_addr_t const pad_sz = new_size - old_size;
992 int err = alloc_anon_mem(pad_sz, &tds, &toffs);
998 err = r->attach(&pad_addr, pad_sz,
999 Rm::F::In_area | Rm::F::Detach_free | Rm::F::RWX,
1012Vfs::mprotect(
const void * ,
size_t ,
int prot)
L4_NOTHROW
1014 return (prot & PROT_WRITE) ? -1 : 0;
1028extern void *l4re_env_posix_vfs_ops __attribute__((alias(
"__rtld_l4re_env_posix_vfs_ops"), visibility(
"default")));
1031 class Real_mount_tree :
public L4Re::Vfs::Mount_tree
1034 explicit Real_mount_tree(
char *n) : Mount_tree(n) {}
1036 void *
operator new (
size_t size)
1037 {
return __rtld_l4re_env_posix_vfs_ops->malloc(size); }
1039 void operator delete (
void *mem)
1040 { __rtld_l4re_env_posix_vfs_ops->free(mem); }
1049 using L4Re::Vfs::Mount_tree;
1050 using L4Re::Vfs::Path;
1057 Path p = root->lookup(Path(path), &base);
1061 Path f = p.strip_first();
1066 char *name = __rtld_l4re_env_posix_vfs_ops->strndup(f.path(), f.length());
1070 auto nt = cxx::make_ref_obj<Real_mount_tree>(name);
1073 __rtld_l4re_env_posix_vfs_ops->free(name);
1077 base->add_child_node(nt);
static Env const * env() noexcept
Returns the initial environment for the current task.
L4::Cap< Log > log() const noexcept
Object-capability to the logging service.
T get() const noexcept
Return the address.
Basic interface for an L4Re::Vfs file system.
The basic interface for an open POSIX file.
Interface for the POSIX backends of an application.
bool is_valid() const noexcept
Test whether the capability is a valid capability index (i.e., not L4_INVALID_CAP).
C++ interface for capabilities.
Basic element type for a double-linked H_list.
Helper type to distinguish the operator new version that does not throw exceptions.
A reference-counting pointer with automatic cleanup.
unsigned int l4_size_t
Unsigned size type.
unsigned long l4_umword_t
Unsigned machine word.
unsigned long l4_addr_t
Address type.
@ L4_EINVAL
Invalid argument.
@ L4_CAP_FPAGE_RO
Read right for capability flexpages.
@ L4_CAP_FPAGE_RW
Read and interface specific 'W' right for capability flexpages.
l4_addr_t l4_trunc_page(l4_addr_t address) L4_NOTHROW
Round an address down to the next lower page boundary.
l4_addr_t l4_round_page(l4_addr_t address) L4_NOTHROW
Round address up to the next page.
#define L4_PAGESIZE
Minimal page size (in bytes).
@ L4_INVALID_ADDR
Invalid address.
#define L4_NOTHROW
Mark a function declaration and definition as never throwing an exception.
Functionality for invoking the kernel debugger.
void outhex32(l4_uint32_t number)
Output a 32-bit unsigned hexadecimal number via the kernel debugger.
void outstring(char const *text)
Output a string via the kernel debugger.
void outdec(l4_mword_t number)
Output a decimal unsigned machine word via the kernel debugger.
L4::Detail::Shared_cap_impl< T, Smart_count_cap< L4_FP_ALL_SPACES > > Shared_cap
Shared capability that implements automatic free and unmap of the capability selector.
Cap< T > make_cap(L4::Cap< T > cap, unsigned rights) noexcept
Make an L4::Ipc::Cap<T> for the given capability and rights.
Cap< T > make_cap_rw(L4::Cap< T > cap) noexcept
Make an L4::Ipc::Cap<T> for the given capability with L4_CAP_FPAGE_RW rights.
L4 low-level kernel interface.
Shared_cap / Shared_del_cap.
@ RW
Readable and writable region.
@ Detach_free
Free the portion of the data space after detach.
@ Search_addr
Search for a suitable address range.
A range of virtual addresses.
l4_addr_t start
First address of the range.
l4_addr_t end
Last address of the range.
Double-linked list of typed H_list_item_t elements.
Low-level assert implementation.
#define l4_assert(expr)
Low-level assert.