22#include "vcon_stream.h"
28#include <l4/cxx/hlist>
30#include <l4/cxx/std_alloc>
32#include <l4/l4re_vfs/backend>
42static int debug_mmap = 1;
43#define DEBUG_LOG(level, dbg...) do { if (level) dbg } while (0)
45#define DEBUG_LOG(level, dbg...) do { } while (0)
53#define USE_BIG_ANON_DS
61class Fd_store :
public L4Re::Core::Fd_store
69class Std_stream : public
L4Re::Core::Vcon_stream
75Fd_store::Fd_store() noexcept
79 static char m[
sizeof(Std_stream)] __attribute__((aligned(
sizeof(
long))));
83 set(0, cxx::ref_ptr(s));
84 set(1, cxx::ref_ptr(s));
85 set(2, cxx::ref_ptr(s));
88class Root_mount_tree :
public L4Re::Vfs::Mount_tree
91 Root_mount_tree() :
L4Re::Vfs::Mount_tree(0) {}
92 void operator delete (
void *) {}
102 : _early_oom(true), _root_mount(), _root(
L4Re::Env::env())
104 _root_mount.add_ref();
106 _root_mount.mount(cxx::ref_ptr(&_root));
107 _cwd = cxx::ref_ptr(&_root);
110 Ref_ptr<L4Re::Vfs::File> rom;
111 _root.openat(
"rom", 0, 0, &rom);
113 _root_mount.create_tree(
"lib/foo", rom);
115 _root.openat(
"lib", 0, 0, &_cwd);
120 int alloc_fd(Ref_ptr<L4Re::Vfs::File>
const &f)
noexcept override;
121 Ref_ptr<L4Re::Vfs::File> free_fd(
int fd)
noexcept override;
122 Ref_ptr<L4Re::Vfs::File> get_root() noexcept override;
123 Ref_ptr<
L4Re::Vfs::File> get_cwd() noexcept override;
124 void set_cwd(Ref_ptr<
L4Re::Vfs::File> const &dir) noexcept override;
125 Ref_ptr<
L4Re::Vfs::File> get_file(
int fd) noexcept override;
126 cxx::Pair<Ref_ptr<
L4Re::Vfs::File>,
int>
127 set_fd(
int fd, Ref_ptr<
L4Re::Vfs::File> const &f = Ref_ptr<>::Nil) noexcept
130 int mmap2(
void *start,
size_t len,
int prot,
int flags,
int fd,
131 off_t offset,
void **ptr) noexcept override;
133 int munmap(
void *start,
size_t len) noexcept override;
134 int mremap(
void *old,
size_t old_sz,
size_t new_sz,
int flags,
135 void **new_addr) noexcept override;
136 int mprotect(const
void *a,
size_t sz,
int prot) noexcept override;
137 int msync(
void *addr,
size_t len,
int flags) noexcept override;
138 int madvise(
void *addr,
size_t len,
int advice) noexcept override;
140 int register_file_system(
L4Re::Vfs::File_system *f) noexcept override;
141 int unregister_file_system(
L4Re::Vfs::File_system *f) noexcept override;
142 L4Re::Vfs::File_system *get_file_system(
char const *fstype) noexcept override;
144 int register_file_factory(
cxx::Ref_ptr<
L4Re::Vfs::File_factory> f) noexcept override;
145 int unregister_file_factory(
cxx::Ref_ptr<
L4Re::Vfs::File_factory> f) noexcept override;
146 Ref_ptr<
L4Re::Vfs::File_factory> get_file_factory(
int proto) noexcept override;
147 Ref_ptr<
L4Re::Vfs::File_factory> get_file_factory(
char const *proto_name) noexcept override;
148 int mount(
char const *path,
cxx::Ref_ptr<
L4Re::Vfs::File> const &dir) noexcept override;
150 void operator delete (
void *) {}
152 void *malloc(
size_t size)
noexcept override {
return Vfs_config::malloc(size); }
153 void free(
void *m)
noexcept override { Vfs_config::free(m); }
156 Root_mount_tree _root_mount;
157 L4Re::Core::Env_dir _root;
158 Ref_ptr<L4Re::Vfs::File> _cwd;
169 File_factory_item() =
default;
170 File_factory_item(File_factory_item
const &) =
delete;
171 File_factory_item &operator = (File_factory_item
const &) =
delete;
183 void align_mmap_start_and_length(
void **start,
size_t *length);
186static inline bool strequal(
char const *a,
char const *b)
188 for (;*a && *a == *b; ++a, ++b)
201 for (File_system *c = _fs_registry; c; c = c->next())
202 if (strequal(c->type(), f->type()))
205 f->next(_fs_registry);
219 File_system **p = &_fs_registry;
221 for (; *p; p = &(*p)->next())
233Vfs::get_file_system(
char const *fstype)
noexcept
235 bool try_dynamic =
true;
239 for (File_system *c = _fs_registry; c; c = c->next())
240 if (strequal(c->type(), fstype))
247 int res = Vfs_config::load_module(fstype);
262 void *x = this->malloc(
sizeof(File_factory_item));
267 _file_factories.push_front(ff);
274 for (
auto p: _file_factories)
278 _file_factories.remove(p);
279 p->~File_factory_item();
287Ref_ptr<L4Re::Vfs::File_factory>
288Vfs::get_file_factory(
int proto)
noexcept
290 for (
auto p: _file_factories)
291 if (p->f->proto() == proto)
294 return Ref_ptr<L4Re::Vfs::File_factory>();
297Ref_ptr<L4Re::Vfs::File_factory>
298Vfs::get_file_factory(
char const *proto_name)
noexcept
300 for (
auto p: _file_factories)
302 auto n = p->f->proto_name();
306 char const *b = proto_name;
307 for (; *a && *b && *a == *b; ++a, ++b)
310 if ((*a == 0) && (*b == 0))
315 return Ref_ptr<L4Re::Vfs::File_factory>();
319Vfs::alloc_fd(Ref_ptr<L4Re::Vfs::File>
const &f)
noexcept
321 int fd = fds.alloc();
331Ref_ptr<L4Re::Vfs::File>
332Vfs::free_fd(
int fd)
noexcept
334 Ref_ptr<L4Re::Vfs::File> f = fds.get(fd);
337 return Ref_ptr<>::Nil;
344Ref_ptr<L4Re::Vfs::File>
345Vfs::get_root() noexcept
347 return cxx::ref_ptr(&_root);
350Ref_ptr<L4Re::Vfs::File>
351Vfs::get_cwd() noexcept
357Vfs::set_cwd(Ref_ptr<L4Re::Vfs::File>
const &dir)
noexcept
364Ref_ptr<L4Re::Vfs::File>
365Vfs::get_file(
int fd)
noexcept
371Vfs::set_fd(
int fd, Ref_ptr<L4Re::Vfs::File>
const &f)
noexcept
373 if (!fds.check_fd(fd))
374 return cxx::pair(Ref_ptr<L4Re::Vfs::File>(Ref_ptr<>::Nil), EBADF);
376 Ref_ptr<L4Re::Vfs::File> old = fds.get(fd);
378 return cxx::pair(old, 0);
382#define GET_FILE_DBG(fd, err) \
383 Ref_ptr<L4Re::Vfs::File> fi = fds.get(fd); \
389#define GET_FILE(fd, err) \
390 Ref_ptr<L4Re::Vfs::File> fi = fds.get(fd); \
396Vfs::align_mmap_start_and_length(
void **start,
size_t *length)
406Vfs::munmap(
void *start,
size_t len)
L4_NOTHROW
409 using namespace L4Re;
418 align_mmap_start_and_length(&start, &len);
422 DEBUG_LOG(debug_mmap, {
429 err = r->detach(
l4_addr_t(start), len, &ds, This_task);
433 switch (err & Rm::Detach_result_mask)
437 L4Re::virt_cap_alloc->take(ds);
439 case Rm::Detached_ds:
441 L4Re::virt_cap_alloc->release(ds);
447 if (!(err & Rm::Detach_again))
455 *ds = L4Re::make_shared_cap<L4Re::Dataspace>(L4Re::virt_cap_alloc);
461 if ((err = Vfs_config::allocator()->alloc(size, ds->get())) < 0)
464 DEBUG_LOG(debug_mmap, {
479#ifdef USE_BIG_ANON_DS
482 ANON_MEM_DS_POOL_SIZE = 256UL << 20,
483 ANON_MEM_MAX_SIZE = 32UL << 20,
488 ANON_MEM_DS_POOL_SIZE = 256UL << 20,
489 ANON_MEM_MAX_SIZE = 0UL << 20,
493 if (size >= ANON_MEM_MAX_SIZE)
496 if ((err = alloc_ds(size, ds)) < 0)
504 return (*ds)->allocate(0, size);
507 if (!_anon_ds.is_valid() || _anon_offset + size >= ANON_MEM_DS_POOL_SIZE)
510 if ((err = alloc_ds(ANON_MEM_DS_POOL_SIZE, ds)) < 0)
521 if (
int err = (*ds)->allocate(_anon_offset, size))
525 *offset = _anon_offset;
526 _anon_offset += size;
531Vfs::mmap2(
void *start,
size_t len,
int prot,
int flags,
int fd, off_t page4k_offset,
534 using namespace L4Re;
537 if (flags & MAP_FIXED)
541 align_mmap_start_and_length(&start, &len);
544 if (flags & 0x1000000)
552 *resptr = (
void*)area;
553 DEBUG_LOG(debug_mmap, {
565 L4Re::Rm::Flags rm_flags(0);
567 if (flags & (MAP_ANONYMOUS | MAP_PRIVATE))
571 int err = alloc_anon_mem(len, &ds, &anon_offset);
575 DEBUG_LOG(debug_mmap, {
584 if (!(flags & MAP_ANONYMOUS))
586 Ref_ptr<L4Re::Vfs::File> fi = fds.get(fd);
604 if (flags & MAP_PRIVATE)
606 DEBUG_LOG(debug_mmap,
outstring(
"COW\n"););
607 int err = ds->copy_in(anon_offset, fds, offset, len);
611 offset = anon_offset;
615 L4Re::virt_cap_alloc->take(fds);
620 offset = anon_offset;
623 if (!(flags & MAP_FIXED) && start == 0)
626 char *data = (
char *)start;
631 if (flags & MAP_FIXED)
635 err = r->reserve_area(&overmap_area, len);
639 rm_flags |= Rm::F::In_area;
641 err = munmap(start, len);
642 if (err && err != -ENOENT)
646 if (!(flags & MAP_FIXED)) rm_flags |= Rm::F::Search_addr;
647 if (prot & PROT_READ) rm_flags |= Rm::F::R;
648 if (prot & PROT_WRITE) rm_flags |= Rm::F::W;
649 if (prot & PROT_EXEC) rm_flags |= Rm::F::X;
651 err = r->attach(&data, len, rm_flags,
657 DEBUG_LOG(debug_mmap, {
673 r->free_area(overmap_area);
701 int e = r->reserve_area(&a, sz, flags);
718 ~Auto_area() { free(); }
723Vfs::mremap(
void *old_addr,
size_t old_size,
size_t new_size,
int flags,
726 using namespace L4Re;
728 DEBUG_LOG(debug_mmap, {
738 if (flags & MREMAP_FIXED && !(flags & MREMAP_MAYMOVE))
745 bool const fixed = flags & MREMAP_FIXED;
746 bool const maymove = flags & MREMAP_MAYMOVE;
756 if (new_size < old_size)
758 *new_addr = old_addr;
759 return munmap((
void*)(oa + new_size), old_size - new_size);
762 if (new_size == old_size)
764 *new_addr = old_addr;
769 Auto_area old_area(r);
770 int err = old_area.reserve(oa, old_size, L4Re::Rm::Flags(0));
775 Auto_area new_area(r);
783 int err = new_area.reserve(na, new_size, L4Re::Rm::Flags(0));
793 unsigned long ts = new_size - old_size;
795 long err = new_area.reserve(ta, ts, L4Re::Rm::Flags(0));
799 L4Re::Rm::Offset toffs;
800 L4Re::Rm::Flags tflags;
803 err = r->find(&ta, &ts, &toffs, &tflags, &tds);
806 if (err == -ENOENT || (err == 0 && (tflags & Rm::F::In_area)))
809 pad_addr = oa + old_size;
810 *new_addr = old_addr;
817 err = new_area.reserve(0, new_size, Rm::F::Search_addr);
821 pad_addr = new_area.a + old_size;
822 *new_addr = (
void *)new_area.a;
826 if (old_area.is_valid())
829 unsigned long s = old_size;
834 for (; r->find(&a, &s, &o, &f, &ds) >= 0 && (!(f & Rm::F::In_area));)
838 auto d = old_area.a - a;
844 if (a + s > old_area.a + old_size)
845 s = old_area.a + old_size - a;
847 l4_addr_t x = a - old_area.a + new_area.a;
849 int err = r->attach(&x, s, Rm::F::In_area | f,
856 L4Re::virt_cap_alloc->take(ds);
858 err = r->detach(a, s, &ds, This_task,
859 Rm::Detach_exact | Rm::Detach_keep);
863 switch (err & Rm::Detach_result_mask)
868 L4Re::virt_cap_alloc->take(ds);
870 case Rm::Detached_ds:
872 L4Re::virt_cap_alloc->release(ds);
881 if (old_size < new_size)
883 l4_addr_t const pad_sz = new_size - old_size;
886 int err = alloc_anon_mem(pad_sz, &tds, &toffs);
892 err = r->attach(&pad_addr, pad_sz,
893 Rm::F::In_area | Rm::F::Detach_free | Rm::F::RWX,
906Vfs::mprotect(
const void *a,
size_t sz,
int prot)
L4_NOTHROW
910 return (prot & PROT_WRITE) ? -1 : 0;
924extern void *l4re_env_posix_vfs_ops __attribute__((alias(
"__rtld_l4re_env_posix_vfs_ops"), visibility(
"default")));
927 class Real_mount_tree :
public L4Re::Vfs::Mount_tree
930 explicit Real_mount_tree(
char *n) : Mount_tree(n) {}
932 void *
operator new (
size_t size)
933 {
return __rtld_l4re_env_posix_vfs_ops->malloc(size); }
935 void operator delete (
void *mem)
936 { __rtld_l4re_env_posix_vfs_ops->free(mem); }
944 using L4Re::Vfs::Mount_tree;
945 using L4Re::Vfs::Path;
952 Path p = root->lookup(Path(path), &base);
956 Path f = p.strip_first();
961 char *name = __rtld_l4re_env_posix_vfs_ops->strndup(f.path(), f.length());
968 __rtld_l4re_env_posix_vfs_ops->free(name);
972 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.
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 oeprator 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_CAP_FPAGE_RO
Read right for capability flex-pages.
@ L4_CAP_FPAGE_RW
Read and interface specific 'W' right for capability flex-pages.
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.
@ Detach_free
Free the portion of the data space after detach.
@ Search_addr
Search for a suitable address range.
Double-linked list of typed H_list_item_t elements.
#define l4_assert(expr)
Low-level assert.