1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#pragma once
4
5#include "mos/platform/platform.h"
6
7#include <mos/io/io_types.h>
8#include <mos/mm/mm_types.h>
9#include <mos/types.h>
10
11typedef struct _io io_t;
12typedef struct _vmap vmap_t; // forward declaration
13
14typedef enum
15{
16 IO_NULL, // null io port
17 IO_FILE, // a file
18 IO_DIR, // a directory (i.e. readdir())
19 IO_IPC, // an IPC channel
20 IO_PIPE, // an end of a pipe
21 IO_CONSOLE, // a console
22} io_type_t;
23
24typedef enum
25{
26 IO_NONE = MEM_PERM_NONE, // 0
27 IO_READABLE = MEM_PERM_READ, // 1 << 0
28 IO_WRITABLE = MEM_PERM_WRITE, // 1 << 1
29 IO_EXECUTABLE = MEM_PERM_EXEC, // 1 << 2
30 IO_SEEKABLE = 1 << 3,
31 IO_MMAPABLE = 1 << 4,
32} io_flags_t;
33
34typedef struct
35{
36 size_t (*read)(io_t *io, void *buf, size_t count);
37 size_t (*write)(io_t *io, const void *buf, size_t count);
38 void (*close)(io_t *io);
39 off_t (*seek)(io_t *io, off_t offset, io_seek_whence_t whence);
40 bool (*mmap)(io_t *io, vmap_t *vmap, off_t offset);
41 bool (*munmap)(io_t *io, vmap_t *vmap, bool *unmapped);
42 void (*get_name)(const io_t *io, char *buf, size_t size);
43} io_op_t;
44
45typedef struct _io
46{
47 bool closed;
48 atomic_t refcount;
49 io_flags_t flags;
50 io_type_t type;
51 const io_op_t *ops;
52} io_t;
53
54extern io_t *const io_null;
55
56void io_init(io_t *io, io_type_t type, io_flags_t flags, const io_op_t *ops);
57
58io_t *io_ref(io_t *io);
59io_t *io_unref(io_t *io);
60__nodiscard bool io_valid(const io_t *io);
61
62size_t io_read(io_t *io, void *buf, size_t count);
63size_t io_pread(io_t *io, void *buf, size_t count, off_t offset);
64size_t io_write(io_t *io, const void *buf, size_t count);
65off_t io_seek(io_t *io, off_t offset, io_seek_whence_t whence);
66off_t io_tell(io_t *io);
67bool io_mmap_perm_check(io_t *io, vm_flags flags, bool is_private);
68bool io_mmap(io_t *io, vmap_t *vmap, off_t offset);
69bool io_munmap(io_t *io, vmap_t *vmap, bool *unmapped);
70void io_get_name(const io_t *io, char *buf, size_t size);
71