Kernel/Userspace: Replace my custom SHM system with SysV shm

This allows xbanan to use shm!
This commit is contained in:
2026-08-08 01:52:12 +03:00
parent 2c4fee4a82
commit fc40820150
23 changed files with 327 additions and 225 deletions
+13 -16
View File
@@ -5,8 +5,7 @@
#include <fcntl.h>
#include <stdlib.h>
#include <sys/banan-os.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/un.h>
@@ -49,12 +48,10 @@ namespace LibAudio
void Audio::clear()
{
if (m_audio_buffer)
munmap(m_audio_buffer, m_smo_size);
shmdt(m_audio_buffer);
m_audio_buffer = nullptr;
if (m_smo_key != -1)
smo_delete(m_smo_key);
m_smo_key = -1;
m_shmid = -1;
if (m_server_fd != -1)
close(m_server_fd);
@@ -68,14 +65,12 @@ namespace LibAudio
clear();
m_server_fd = other.m_server_fd;
m_smo_key = other.m_smo_key;
m_smo_size = other.m_smo_size;
m_shmid = other.m_shmid;
m_audio_buffer = other.m_audio_buffer;
m_audio_loader = BAN::move(other.m_audio_loader);
other.m_server_fd = -1;
other.m_smo_key = -1;
other.m_smo_size = 0;
other.m_shmid = -1;
other.m_audio_buffer = nullptr;
return *this;
@@ -83,14 +78,16 @@ namespace LibAudio
BAN::ErrorOr<void> Audio::initialize(uint32_t total_samples)
{
m_smo_size = sizeof(AudioBuffer) + total_samples * sizeof(AudioBuffer::sample_t);
const size_t shm_size = sizeof(AudioBuffer) + total_samples * sizeof(AudioBuffer::sample_t);
m_smo_key = smo_create(m_smo_size, PROT_READ | PROT_WRITE);
if (m_smo_key == -1)
m_shmid = shmget(IPC_PRIVATE, shm_size, 0666);
if (m_shmid == -1)
return BAN::Error::from_errno(errno);
m_audio_buffer = static_cast<AudioBuffer*>(smo_map(m_smo_key));
if (m_audio_buffer == nullptr)
m_audio_buffer = static_cast<AudioBuffer*>(shmat(m_shmid, nullptr, 0));
shmctl(m_shmid, IPC_RMID, nullptr);
if (m_audio_buffer == SHM_FAILED)
return BAN::Error::from_errno(errno);
new (m_audio_buffer) AudioBuffer();
memset(m_audio_buffer->samples, 0, total_samples * sizeof(AudioBuffer::sample_t));
@@ -124,7 +121,7 @@ namespace LibAudio
const LibAudio::Packet packet {
.type = LibAudio::Packet::RegisterBuffer,
.parameter = static_cast<uint64_t>(m_smo_key),
.parameter = static_cast<uint64_t>(m_shmid),
};
const ssize_t nsend = send(m_server_fd, &packet, sizeof(packet), 0);
@@ -62,8 +62,7 @@ namespace LibAudio
BAN::UniqPtr<AudioLoader> m_audio_loader;
long m_smo_key { -1 };
size_t m_smo_size { 0 };
int m_shmid { -1 };
AudioBuffer* m_audio_buffer { nullptr };
};
@@ -43,13 +43,6 @@ int poweroff(int command);
int load_keymap(const char* path);
// Create shared memory object and return its key or -1 on error
long smo_create(size_t size, int prot);
// Delete shared memory object such that it will be no longer accessible with smo_map(). Existing mappings are still valid
int smo_delete(long key);
// Map shared memory object defined by its key and return address or null on error. Mappings can be unmapped using munmap()
void* smo_map(long key);
__END_DECLS
#endif
+9 -7
View File
@@ -22,13 +22,15 @@ struct ipc_perm
mode_t mode; /* Read/write permission. */
};
#define IPC_CREAT 0x01
#define IPC_EXCL 0x02
#define IPC_NOWAIT 0x04
#define IPC_PRIVATE 0x08
#define IPC_RMID 0x10
#define IPC_SET 0x20
#define IPC_STAT 0x40
#define IPC_CREAT 01000
#define IPC_EXCL 02000
#define IPC_NOWAIT 04000
#define IPC_PRIVATE 0
#define IPC_RMID 1
#define IPC_SET 2
#define IPC_STAT 3
key_t ftok(const char* path, int id);
+3 -3
View File
@@ -12,14 +12,14 @@ __BEGIN_DECLS
#define __need_time_t
#include <sys/types.h>
#include <stdint.h>
#include <sys/ipc.h>
#include <unistd.h>
#define SHM_RDONLY 0x01
#define SHM_RND 0x02
#define SHMLBA (sysconf(_SC_PAGE_SIZE))
#define SHM_FAILED ((void*)(intptr_t)-1)
#define SHMLBA (getpagesize())
typedef unsigned int shmatt_t;
@@ -72,9 +72,10 @@ __BEGIN_DECLS
O(SYS_PSELECT, pselect) \
O(SYS_PPOLL, ppoll) \
O(SYS_FTRUNCATE, ftruncate) \
O(SYS_SMO_CREATE, smo_create) \
O(SYS_SMO_DELETE, smo_delete) \
O(SYS_SMO_MAP, smo_map) \
O(SYS_SHMAT, shmat) \
O(SYS_SHMDT, shmdt) \
O(SYS_SHMGET, shmget) \
O(SYS_SHMCTL, shmctl) \
O(SYS_GETSOCKNAME, getsockname) \
O(SYS_GETPEERNAME, getpeername) \
O(SYS_GETSOCKOPT, getsockopt) \
-18
View File
@@ -16,21 +16,3 @@ int load_keymap(const char* path)
{
return syscall(SYS_LOAD_KEYMAP, path);
}
long smo_create(size_t size, int prot)
{
return syscall(SYS_SMO_CREATE, size, prot);
}
int smo_delete(long key)
{
return syscall(SYS_SMO_DELETE, key);
}
void* smo_map(long key)
{
long ret = syscall(SYS_SMO_MAP, key);
if (ret < 0)
return nullptr;
return reinterpret_cast<void*>(ret);
}
+23 -8
View File
@@ -1,11 +1,26 @@
#include <BAN/Debug.h>
#include <errno.h>
#include <sys/shm.h>
#include <sys/syscall.h>
#include <unistd.h>
#define TODO_FUNC(type, name, ...) type name(__VA_ARGS__) { dwarnln("TODO: " #name); errno = ENOTSUP; return (type)-1; }
void* shmat(int shmid, const void* shmaddr, int shmflg)
{
const auto result = syscall(SYS_SHMAT, shmid, shmaddr, shmflg);
if (result == -1)
return SHM_FAILED;
return reinterpret_cast<void*>(result);
}
TODO_FUNC(void*, shmat, int, const void*, int)
TODO_FUNC(int, shmctl, int, int, struct shmid_ds*)
TODO_FUNC(int, shmdt, const void*)
TODO_FUNC(int, shmget, key_t, size_t, int)
int shmctl(int shmid, int cmd, struct shmid_ds* buf)
{
return syscall(SYS_SHMCTL, shmid, cmd, buf);
}
int shmdt(const void* shmaddr)
{
return syscall(SYS_SHMDT, shmaddr);
}
int shmget(key_t key, size_t size, int shmflg)
{
return syscall(SYS_SHMGET, key, size, shmflg);
}
+10 -11
View File
@@ -4,9 +4,8 @@
#include <fcntl.h>
#include <stdlib.h>
#include <sys/banan-os.h>
#include <sys/epoll.h>
#include <sys/mman.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <time.h>
@@ -165,7 +164,7 @@ namespace LibGUI
if (width == m_width)
{
copy_func(
&m_framebuffer_smo[y * m_width],
&m_framebuffer_shm[y * m_width],
&m_texture.pixels()[y * m_width],
width * height * sizeof(uint32_t)
);
@@ -173,7 +172,7 @@ namespace LibGUI
else for (uint32_t y_off = 0; y_off < height; y_off++)
{
copy_func(
&m_framebuffer_smo[(y + y_off) * m_width + x],
&m_framebuffer_shm[(y + y_off) * m_width + x],
&m_texture.pixels()[(y + y_off) * m_width + x],
width * sizeof(uint32_t)
);
@@ -311,31 +310,31 @@ namespace LibGUI
void Window::cleanup()
{
munmap(m_framebuffer_smo, m_width * m_height * 4);
shmdt(m_framebuffer_shm);
close(m_server_fd);
close(m_epoll_fd);
}
BAN::ErrorOr<bool> Window::handle_resize_event(const EventPacket::ResizeWindowEvent& event)
{
void* framebuffer_addr = smo_map(event.smo_key);
if (framebuffer_addr == nullptr)
void* framebuffer_addr = shmat(event.shmid, nullptr, 0);
if (framebuffer_addr == SHM_FAILED)
{
if (errno == ENOENT)
return false;
return BAN::Error::from_errno(errno);
}
if (m_framebuffer_smo)
munmap(m_framebuffer_smo, m_width * m_height * 4);
m_framebuffer_smo = nullptr;
if (m_framebuffer_shm)
shmdt(m_framebuffer_shm);
m_framebuffer_shm = nullptr;
TRY(m_texture.resize(event.width, event.height));
if (m_root_widget)
TRY(m_root_widget->set_fixed_geometry({ 0, 0, event.width, event.height }));
m_framebuffer_smo = static_cast<uint32_t*>(framebuffer_addr);
m_framebuffer_shm = static_cast<uint32_t*>(framebuffer_addr);
m_width = event.width;
m_height = event.height;
@@ -333,7 +333,7 @@ namespace LibGUI
ResizeWindowEvent,
uint32_t, width,
uint32_t, height,
long, smo_key
long, shmid
);
DEFINE_PACKET_EXTRA(
@@ -115,7 +115,7 @@ namespace LibGUI
Attributes m_attributes;
uint32_t* m_framebuffer_smo { nullptr };
uint32_t* m_framebuffer_shm { nullptr };
uint32_t m_width { 0 };
uint32_t m_height { 0 };
@@ -8,13 +8,13 @@
#include <kernel/FS/VirtualFileSystem.h>
#include <kernel/Process.h>
#else
#include <fcntl.h>
#include <limits.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <ctype.h>
#include <fcntl.h>
namespace LibInput
{