Compare commits
6 Commits
bfcca3a220
...
b01aa72f17
Author | SHA1 | Date | |
---|---|---|---|
b01aa72f17 | |||
d41fb85466 | |||
5aa042a5f2 | |||
f150425222 | |||
842b212685 | |||
641b65da0f |
@ -292,6 +292,15 @@ static void mount_tmpfs()
|
|||||||
if (chmod("/tmp", 01777) < 0) exit(255);
|
if (chmod("/tmp", 01777) < 0) exit(255);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void mount_shmfs()
|
||||||
|
{
|
||||||
|
if (mkdir("/dev/shm", 0755) < 0) exit(255);
|
||||||
|
|
||||||
|
if (mount("/dev/shm", "tmpfs", "tmpfs") < 0) exit(255);
|
||||||
|
|
||||||
|
if (chmod("/dev/shm", 01777) < 0) exit(255);
|
||||||
|
}
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
if (getpid() != 1)
|
if (getpid() != 1)
|
||||||
@ -307,6 +316,7 @@ int main()
|
|||||||
stderr = fopen("/dev/console", "w");
|
stderr = fopen("/dev/console", "w");
|
||||||
|
|
||||||
mount_tmpfs();
|
mount_tmpfs();
|
||||||
|
mount_shmfs();
|
||||||
|
|
||||||
umask(022);
|
umask(022);
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#include <fcntl.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/wait.h>
|
#include <sys/wait.h>
|
||||||
@ -5,15 +6,27 @@
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
void* address = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_SHARED, -1, 0);
|
int fd = shm_open("/shared", O_CREAT | O_RDWR, 0666);
|
||||||
|
if (fd < 0)
|
||||||
|
{
|
||||||
|
perror("shm_open");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
ftruncate(fd, PAGE_SIZE);
|
||||||
|
|
||||||
|
void* address = mmap(nullptr, PAGE_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
|
||||||
if (address == MAP_FAILED)
|
if (address == MAP_FAILED)
|
||||||
{
|
{
|
||||||
perror("mmap");
|
perror("mmap");
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int* ptr = (int*)address;
|
char* ptr = (char*)address;
|
||||||
*ptr = 16;
|
|
||||||
|
printf("the value is %c\n", *ptr);
|
||||||
|
|
||||||
|
*ptr = 'a';
|
||||||
|
|
||||||
pid_t child = fork();
|
pid_t child = fork();
|
||||||
if (child < 0)
|
if (child < 0)
|
||||||
@ -23,13 +36,13 @@ int main()
|
|||||||
}
|
}
|
||||||
if (child == 0)
|
if (child == 0)
|
||||||
{
|
{
|
||||||
*ptr = 32;
|
*ptr = 'e';
|
||||||
_exit(0);
|
_exit(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
waitpid(child, NULL, 0);
|
waitpid(child, NULL, 0);
|
||||||
|
|
||||||
printf("the value is %d\n", *ptr);
|
printf("the value is %c\n", *ptr);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -39,6 +39,11 @@ Result<u64> FramebufferDevice::query_shared_memory(off_t offset, usize count)
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto* shm = g_shared_memory_map.try_get_ref(*m_shmid);
|
auto* shm = g_shared_memory_map.try_get_ref(*m_shmid);
|
||||||
|
if (!shm)
|
||||||
|
{
|
||||||
|
m_shmid = {};
|
||||||
|
return query_shared_memory(offset, count);
|
||||||
|
}
|
||||||
if (shm->offset > offset)
|
if (shm->offset > offset)
|
||||||
{
|
{
|
||||||
TRY(shm->grow_backward(Framebuffer::ptr() + offset, (shm->offset - offset) / ARCH_PAGE_SIZE));
|
TRY(shm->grow_backward(Framebuffer::ptr() + offset, (shm->offset - offset) / ARCH_PAGE_SIZE));
|
||||||
|
@ -144,6 +144,12 @@ namespace TmpFS
|
|||||||
}
|
}
|
||||||
|
|
||||||
auto* shm = g_shared_memory_map.try_get_ref(*m_shmid);
|
auto* shm = g_shared_memory_map.try_get_ref(*m_shmid);
|
||||||
|
if (!shm)
|
||||||
|
{
|
||||||
|
m_shmid = {};
|
||||||
|
return query_shared_memory(offset, count);
|
||||||
|
}
|
||||||
|
|
||||||
if (shm->offset > offset)
|
if (shm->offset > offset)
|
||||||
{
|
{
|
||||||
TRY(shm->grow_backward(m_data_buffer.data() + offset, (shm->offset - offset) / ARCH_PAGE_SIZE));
|
TRY(shm->grow_backward(m_data_buffer.data() + offset, (shm->offset - offset) / ARCH_PAGE_SIZE));
|
||||||
|
@ -107,12 +107,16 @@ Result<void> SharedMemory::map(u64 virt, int flags, off_t _offset, usize count)
|
|||||||
|
|
||||||
void SharedMemory::free()
|
void SharedMemory::free()
|
||||||
{
|
{
|
||||||
if (inode && (prot & PROT_WRITE))
|
if ((inode || device) && (prot & PROT_WRITE))
|
||||||
{
|
{
|
||||||
for (u64 i = 0; i < frames.size(); i++)
|
for (u64 i = 0; i < frames.size(); i++)
|
||||||
{
|
{
|
||||||
|
if (inode)
|
||||||
inode->write((u8*)MMU::translate_physical_address(frames[i]), offset + (i * ARCH_PAGE_SIZE),
|
inode->write((u8*)MMU::translate_physical_address(frames[i]), offset + (i * ARCH_PAGE_SIZE),
|
||||||
ARCH_PAGE_SIZE);
|
ARCH_PAGE_SIZE);
|
||||||
|
if (device)
|
||||||
|
device->write((u8*)MMU::translate_physical_address(frames[i]), offset + (i * ARCH_PAGE_SIZE),
|
||||||
|
ARCH_PAGE_SIZE);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,6 +14,9 @@ struct SharedMemory
|
|||||||
SharedPtr<Device> device {};
|
SharedPtr<Device> device {};
|
||||||
int refs { 0 };
|
int refs { 0 };
|
||||||
|
|
||||||
|
SharedMemory() = default;
|
||||||
|
SharedMemory(SharedMemory&&) = default;
|
||||||
|
|
||||||
static Result<u64> create(u8* mem, off_t offset, usize count);
|
static Result<u64> create(u8* mem, off_t offset, usize count);
|
||||||
|
|
||||||
Result<void> grow_forward(u8* mem, usize count);
|
Result<void> grow_forward(u8* mem, usize count);
|
||||||
@ -22,6 +25,8 @@ struct SharedMemory
|
|||||||
Result<void> map(u64 virt, int flags, off_t offset, usize count);
|
Result<void> map(u64 virt, int flags, off_t offset, usize count);
|
||||||
|
|
||||||
void free();
|
void free();
|
||||||
|
|
||||||
|
~SharedMemory() = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
extern HashMap<u64, SharedMemory> g_shared_memory_map;
|
extern HashMap<u64, SharedMemory> g_shared_memory_map;
|
||||||
|
@ -50,6 +50,7 @@ Result<u64> sys_mmap(Registers*, SyscallArgs args)
|
|||||||
}
|
}
|
||||||
shmem = g_shared_memory_map.try_get_ref(shmid);
|
shmem = g_shared_memory_map.try_get_ref(shmid);
|
||||||
shmem->refs++;
|
shmem->refs++;
|
||||||
|
shmem->prot |= params.prot;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 address;
|
u64 address;
|
||||||
|
@ -28,6 +28,12 @@ extern "C"
|
|||||||
/* Write modified shared memory back to its associated file. */
|
/* Write modified shared memory back to its associated file. */
|
||||||
int msync(void* addr, size_t len, int flags);
|
int msync(void* addr, size_t len, int flags);
|
||||||
|
|
||||||
|
/* Create a new POSIX shared memory object. */
|
||||||
|
int shm_open(const char* name, int oflag, mode_t mode);
|
||||||
|
|
||||||
|
/* Delete a POSIX shared memory object from the file system. */
|
||||||
|
int shm_unlink(const char* name);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -1,6 +1,8 @@
|
|||||||
#include <bits/errno-return.h>
|
#include <bits/errno-return.h>
|
||||||
#include <bits/mmap.h>
|
#include <bits/mmap.h>
|
||||||
|
#include <fcntl.h>
|
||||||
#include <luna/Heap.h>
|
#include <luna/Heap.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <sys/mman.h>
|
#include <sys/mman.h>
|
||||||
#include <sys/syscall.h>
|
#include <sys/syscall.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -25,4 +27,18 @@ extern "C"
|
|||||||
long rc = syscall(SYS_msync, addr, len);
|
long rc = syscall(SYS_msync, addr, len);
|
||||||
__errno_return(rc, int);
|
__errno_return(rc, int);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int shm_open(const char* name, int oflag, mode_t mode)
|
||||||
|
{
|
||||||
|
char buf[BUFSIZ];
|
||||||
|
snprintf(buf, sizeof(buf), "/dev/shm%s", name);
|
||||||
|
return open(buf, oflag | O_CLOEXEC, mode);
|
||||||
|
}
|
||||||
|
|
||||||
|
int shm_unlink(const char* name)
|
||||||
|
{
|
||||||
|
char buf[BUFSIZ];
|
||||||
|
snprintf(buf, sizeof(buf), "/dev/shm%s", name);
|
||||||
|
return unlink(buf);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,7 @@ struct Shareable
|
|||||||
return m_ref_count == 0;
|
return m_ref_count == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
Atomic<int> m_ref_count { 1 };
|
Atomic<int> m_ref_count { 0 };
|
||||||
};
|
};
|
||||||
|
|
||||||
template <typename T> class SharedPtr
|
template <typename T> class SharedPtr
|
||||||
@ -36,6 +36,7 @@ template <typename T> class SharedPtr
|
|||||||
|
|
||||||
SharedPtr(T* ptr) : m_ptr(ptr)
|
SharedPtr(T* ptr) : m_ptr(ptr)
|
||||||
{
|
{
|
||||||
|
if (m_ptr) shareable()->ref();
|
||||||
}
|
}
|
||||||
|
|
||||||
SharedPtr(const SharedPtr<T>& other) : m_ptr(other.m_ptr)
|
SharedPtr(const SharedPtr<T>& other) : m_ptr(other.m_ptr)
|
||||||
@ -50,7 +51,6 @@ template <typename T> class SharedPtr
|
|||||||
|
|
||||||
template <typename Tp> operator SharedPtr<Tp>()
|
template <typename Tp> operator SharedPtr<Tp>()
|
||||||
{
|
{
|
||||||
if (m_ptr) shareable()->ref();
|
|
||||||
return { (Tp*)m_ptr };
|
return { (Tp*)m_ptr };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user