summaryrefslogtreecommitdiff
path: root/kernel/syscall.cpp
blob: e91d12dce26991193210facde2acece4ba8cc2b8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <mercury/kernel/application.hpp>
#include <mercury/kernel/framebuffer.hpp>
#include <mercury/kernel/paging.hpp>

using namespace mercury::kernel;

extern "C" uint32_t syscall_encode_color(uint32_t c) {
  return (uint32_t)framebuffer::encode_color(
    c & 0xff, (c >> 8) & 0xff, (c >> 16) & 0xff
  );
}

extern "C" uint64_t syscall_get_fb_vaddr() {
  auto *app = application::running_app;
  if (app->framebuffer_vaddr != 0)
    return app->framebuffer_vaddr;
  uint64_t fb_len = framebuffer::dword_pitch * framebuffer::height * 4;
  uint64_t fb_pages = (fb_len - 1) / 0x200000 + 1;
  uint64_t vaddr = app->get_free_vaddr_pages(fb_pages);
  for (uint64_t i = 0; i < fb_pages; ++i) {
    uint64_t paddr = paging::take_2mib_pram_page();
    app->map_page(vaddr + i * 0x200000, paddr, true, false, true);
  }
  app->framebuffer_vaddr = vaddr;
  return vaddr;
}

extern "C" uint64_t syscall_get_fb_dims() {
  return (uint64_t)(uint32_t)framebuffer::width +
    ((uint64_t)(uint32_t)framebuffer::height << 32);
}

extern "C" uint32_t syscall_get_fb_pitch() {
  return (uint32_t)framebuffer::dword_pitch;
}

extern "C" void syscall_copy_framebuffer() {
  auto *app = application::running_app;
  if (app->framebuffer_vaddr != 0) {
    const uint32_t *source = (const uint32_t *)app->framebuffer_vaddr;
    for (int y = 0; y < framebuffer::height; ++y)
      for (int x = 0; x < framebuffer::width; ++x)
        framebuffer::vaddr[y * framebuffer::dword_pitch + x]
          = source[y * framebuffer::dword_pitch + x];
  }
}