The Pedigree Project  0.1
pedigree-syscalls.cc
1 /*
2  * Copyright (c) 2008-2014, Pedigree Developers
3  *
4  * Please see the CONTRIB file in the root of the source tree for a full
5  * list of contributors.
6  *
7  * Permission to use, copy, modify, and distribute this software for any
8  * purpose with or without fee is hereby granted, provided that the above
9  * copyright notice and this permission notice appear in all copies.
10  *
11  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18  */
19 
20 #include "modules/system/config/Config.h"
21 #include "modules/system/vfs/File.h"
23 #include "modules/system/vfs/Symlink.h"
24 #include "modules/system/vfs/VFS.h"
25 #include "pedigree/kernel/Log.h"
26 #include "pedigree/kernel/linker/KernelElf.h"
27 #include "pedigree/kernel/machine/Display.h"
28 #include "pedigree/kernel/machine/Framebuffer.h"
29 #include "pedigree/kernel/machine/InputManager.h"
30 #include "pedigree/kernel/machine/KeymapManager.h"
31 #include "pedigree/kernel/process/PerProcessorScheduler.h"
32 #include "pedigree/kernel/process/Process.h"
33 #include "pedigree/kernel/process/eventNumbers.h"
34 #include "pedigree/kernel/processor/Processor.h"
35 #include "pedigree/kernel/syscallError.h"
36 
37 #include "pedigree/kernel/ServiceManager.h"
38 #include "pedigree/kernel/graphics/Graphics.h"
39 #include "pedigree/kernel/graphics/GraphicsService.h"
40 
41 #include "modules/system/users/User.h"
42 #include "modules/system/users/UserManager.h"
43 
44 #define PEDIGREEC_WITHIN_KERNEL
45 #include "pedigree-syscalls.h"
46 
47 static const char *pConfigPermissionError =
48  "insufficient permissions: only root allowed";
49 
50 struct blitargs
51 {
52  Graphics::Buffer *pBuffer;
53  uint32_t srcx, srcy, destx, desty, width, height;
54 } PACKED;
55 
56 struct drawargs
57 {
58  void *a;
59  uintptr_t b, c, d, e, f, g, h;
60 } PACKED;
61 
62 struct createargs
63 {
64  void *pFramebuffer;
65  void *pReturnProvider;
66  size_t x, y, w, h;
67 } PACKED;
68 
69 struct fourargs
70 {
71  uintptr_t a, b, c, d;
72 } PACKED;
73 
74 struct sixargs
75 {
76  uintptr_t a, b, c, d, e, f;
77 } PACKED;
78 
79 #define MAX_RESULTS 32
80 static Config::Result *g_Results[MAX_RESULTS];
81 
82 void pedigree_config_init()
83 {
84  ByteSet(g_Results, 0, sizeof(Config::Result *) * MAX_RESULTS);
85 }
86 
87 void pedigree_config_getcolname(
88  size_t resultIdx, size_t n, char *buf, size_t bufsz)
89 {
90  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
91  return;
92  String str = g_Results[resultIdx]->getColumnName(n);
93  StringCopyN(buf, static_cast<const char *>(str), bufsz);
94 }
95 
96 void pedigree_config_getstr(
97  size_t resultIdx, size_t row, size_t n, char *buf, size_t bufsz)
98 {
99  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
100  return;
101  String str = g_Results[resultIdx]->getStr(row, n);
102  StringCopyN(buf, static_cast<const char *>(str), bufsz);
103 }
104 
105 void pedigree_config_getstr(
106  size_t resultIdx, size_t row, const char *col, char *buf, size_t bufsz)
107 {
108  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
109  return;
110  String str = g_Results[resultIdx]->getStr(row, col);
111  StringCopyN(buf, static_cast<const char *>(str), bufsz);
112 }
113 
114 int pedigree_config_getnum(size_t resultIdx, size_t row, size_t n)
115 {
116  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
117  return 0;
118  return static_cast<int>(g_Results[resultIdx]->getNum(row, n));
119 }
120 
121 int pedigree_config_getnum(size_t resultIdx, size_t row, const char *col)
122 {
123  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
124  return 0;
125  return static_cast<int>(g_Results[resultIdx]->getNum(row, col));
126 }
127 
128 int pedigree_config_getbool(size_t resultIdx, size_t row, size_t n)
129 {
130  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
131  return 0;
132  return static_cast<int>(g_Results[resultIdx]->getBool(row, n));
133 }
134 
135 int pedigree_config_getbool(size_t resultIdx, size_t row, const char *col)
136 {
137  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
138  return 0;
139  return static_cast<int>(g_Results[resultIdx]->getNum(row, col));
140 }
141 
142 int pedigree_config_query(const char *query)
143 {
144  for (size_t i = 0; i < MAX_RESULTS; i++)
145  {
146  if (g_Results[i] == 0)
147  {
148  // Check for user performing the query: only root has config access
150  .getCurrentThread()
151  ->getParent()
152  ->getUser()
153  ->getId())
154  {
155  char *pError =
156  new char[StringLength(pConfigPermissionError) + 1];
157  StringCopy(pError, pConfigPermissionError);
158  g_Results[i] = new Config::Result(0, 0, 0, pError, -1);
159  }
160  else
161  g_Results[i] = Config::instance().query(query);
162 
163  return i;
164  }
165  }
166  ERROR("Insufficient free resultsets.");
167  return -1;
168 }
169 
170 void pedigree_config_freeresult(size_t resultIdx)
171 {
172  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
173  return;
174  delete g_Results[resultIdx];
175  g_Results[resultIdx] = 0;
176 }
177 
178 int pedigree_config_numcols(size_t resultIdx)
179 {
180  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
181  return -1;
182  return g_Results[resultIdx]->cols();
183 }
184 
185 int pedigree_config_numrows(size_t resultIdx)
186 {
187  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
188  return -1;
189  return g_Results[resultIdx]->rows();
190 }
191 
192 int pedigree_config_was_successful(size_t resultIdx)
193 {
194  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
195  return -1;
196  return (g_Results[resultIdx]->succeeded()) ? 0 : -1;
197 }
198 
199 void pedigree_config_get_error_message(size_t resultIdx, char *buf, int buflen)
200 {
201  if (resultIdx >= MAX_RESULTS || g_Results[resultIdx] == 0)
202  return;
203  StringCopyN(buf, g_Results[resultIdx]->errorMessage(), buflen);
204 }
205 
206 // Module handling functions
207 
208 // Load a module
209 void pedigree_module_load(char *_file)
210 {
211  // Attempt to find the file, first!
212  File *file = VFS::instance().find(
213  String(_file),
214  Processor::information().getCurrentThread()->getParent()->getCwd());
215  if (!file)
216  {
217  // Error - not found.
218  SYSCALL_ERROR(DoesNotExist);
219  return;
220  }
221 
222  while (file->isSymlink())
223  file = Symlink::fromFile(file)->followLink();
224 
225  if (file->isDirectory())
226  {
227  // Error - is directory.
228  SYSCALL_ERROR(IsADirectory);
229  return;
230  }
231 
232  // Map the module in the memory
233  uintptr_t buffer = 0;
235  file, buffer, file->getSize(), MemoryMappedObject::Read);
237  reinterpret_cast<uint8_t *>(buffer), file->getSize(), true);
239 }
240 
241 // Unload a module
242 void pedigree_module_unload(char *name)
243 {
244  KernelElf::instance().unloadModule(name, true);
245 }
246 
247 // Check if a module is loaded
248 int pedigree_module_is_loaded(char *name)
249 {
250  return (KernelElf::instance().moduleIsLoaded(name) ? 1 : 0);
251 }
252 
253 // Get the first module that depends on the specified module
254 int pedigree_module_get_depending(char *name, char *buf, size_t bufsz)
255 {
256  char *dep = KernelElf::instance().getDependingModule(name);
257  if (dep)
258  StringCopyN(buf, dep, bufsz);
259  else
260  return 0;
261  return 1;
262 }
263 
264 void pedigree_input_install_callback(void *p, uint32_t type, uintptr_t param)
265 {
266  // First parameter is now obsolete, gotta remove it some time...
268  static_cast<InputManager::CallbackType>(type),
269  reinterpret_cast<InputManager::callback_t>(p), 0,
270  Processor::information().getCurrentThread(), param);
271 }
272 
273 void pedigree_input_remove_callback(void *p)
274 {
275  // First parameter is now obsolete, gotta remove it some time...
277  reinterpret_cast<InputManager::callback_t>(p), 0,
278  Processor::information().getCurrentThread());
279 }
280 
281 void pedigree_input_inhibit_events(int inhibit)
282 {
283  Thread *pThread = Processor::information().getCurrentThread();
284  pThread->inhibitEvent(EventNumbers::InputEvent, inhibit == 1);
285 }
286 
287 int pedigree_load_keymap(uint32_t *buf, size_t len)
288 {
290  if (!KeymapManager::instance().useCompiledKeymap(buf, len))
291  {
292  return -1;
293  }
294  else
295  {
296  return 0;
297  }
298 }
299 
300 int pedigree_gfx_get_provider(void *p)
301 {
302  if (!p)
303  return -1;
304 
306 
307  // Grab the current graphics provider for the system, use it to display the
308  // splash screen to the user.
310  ServiceFeatures *pFeatures =
311  ServiceManager::instance().enumerateOperations(String("graphics"));
312  Service *pService =
313  ServiceManager::instance().getService(String("graphics"));
314  if (pFeatures->provides(ServiceFeatures::probe))
315  {
316  if (pService)
317  {
318  if (!pService->serve(
320  reinterpret_cast<void *>(&gfxProvider),
321  sizeof(gfxProvider)))
322  return -1;
323  }
324  else
325  return -1;
326  }
327  else
328  return -1;
329 
330  MemoryCopy(p, &gfxProvider, sizeof(gfxProvider));
331 
332  return 0;
333 }
334 
335 int pedigree_gfx_get_curr_mode(void *p, void *sm)
336 {
337  if (!p)
338  return -1;
339 
342  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
343 
344  Display::ScreenMode mode;
345  pProvider->pDisplay->getCurrentScreenMode(mode);
346 
347  MemoryCopy(sm, &mode, sizeof(mode));
348 
349  return 0;
350 }
351 
352 uintptr_t pedigree_gfx_get_raw_buffer(void *p)
353 {
354  if (!p)
355  return -1;
356 
359  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
360 
361  void *raw = pProvider->pFramebuffer->getRawBuffer();
362  physical_uintptr_t ret = 0;
363 
364  // Return a physical address, the userspace application can mmap this
365  // with MAP_PHYS_OFFSET. This allows us to avoid having to map in areas
366  // of the kernel address space that the application can use.
367  VirtualAddressSpace &va = Processor::information().getVirtualAddressSpace();
368  if (va.isMapped(raw))
369  {
370  NOTICE("hi");
371  size_t flags = 0;
372  va.getMapping(raw, ret, flags);
373  }
374 
375  return ret;
376 }
377 
378 int pedigree_gfx_create_buffer(void *p, void **b, void *args)
379 {
380  if (!p)
381  return -1;
382 
383  fourargs *pArgs = reinterpret_cast<fourargs *>(args);
384 
387  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
388 
389  *b = pProvider->pFramebuffer->createBuffer(
390  reinterpret_cast<void *>(pArgs->a),
391  static_cast<Graphics::PixelFormat>(pArgs->b), pArgs->c, pArgs->d);
392 
393  return *b ? 0 : -1;
394 }
395 
396 int pedigree_gfx_destroy_buffer(void *p, void *b)
397 {
398  if (!p)
399  return -1;
400 
403  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
404 
405  pProvider->pFramebuffer->destroyBuffer(
406  reinterpret_cast<Graphics::Buffer *>(b));
407 
408  return 0;
409 }
410 
411 void pedigree_gfx_redraw(void *p, void *args)
412 {
413  if (!p)
414  return;
415 
416  sixargs *pArgs = reinterpret_cast<sixargs *>(args);
417 
420  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
421 
422  pProvider->pFramebuffer->redraw(
423  pArgs->a, pArgs->b, pArgs->c, pArgs->d, pArgs->e);
424 }
425 
426 void pedigree_gfx_blit(void *p, void *args)
427 {
428  if (!p || !args)
429  return;
430 
431  blitargs *pArgs = reinterpret_cast<blitargs *>(args);
432 
435  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
436 
437  pProvider->pFramebuffer->blit(
438  pArgs->pBuffer, pArgs->srcx, pArgs->srcy, pArgs->destx, pArgs->desty,
439  pArgs->width, pArgs->height);
440 }
441 
442 void pedigree_gfx_set_pixel(
443  void *p, uint32_t x, uint32_t y, uint32_t colour, uint32_t fmt)
444 {
445  if (!p)
446  return;
447 
450  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
451 
452  pProvider->pFramebuffer->setPixel(
453  x, y, colour, static_cast<Graphics::PixelFormat>(fmt));
454 }
455 
456 void pedigree_gfx_rect(void *p, void *args)
457 {
458  if (!p || !args)
459  return;
460 
461  sixargs *pArgs = reinterpret_cast<sixargs *>(args);
462 
465  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
466 
467  pProvider->pFramebuffer->rect(
468  pArgs->a, pArgs->b, pArgs->c, pArgs->d, pArgs->e,
469  static_cast<Graphics::PixelFormat>(pArgs->f));
470 }
471 
472 void pedigree_gfx_copy(void *p, void *args)
473 {
474  if (!p || !args)
475  return;
476 
477  sixargs *pArgs = reinterpret_cast<sixargs *>(args);
478 
481  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
482 
483  pProvider->pFramebuffer->copy(
484  pArgs->a, pArgs->b, pArgs->c, pArgs->d, pArgs->e, pArgs->f);
485 }
486 
487 void pedigree_gfx_line(void *p, void *args)
488 {
489  if (!p || !args)
490  return;
491 
492  sixargs *pArgs = reinterpret_cast<sixargs *>(args);
493 
496  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
497 
498  pProvider->pFramebuffer->line(
499  pArgs->a, pArgs->b, pArgs->c, pArgs->d, pArgs->e,
500  static_cast<Graphics::PixelFormat>(pArgs->f));
501 }
502 
503 void pedigree_gfx_draw(void *p, void *args)
504 {
505  if (!p || !args)
506  return;
507 
508  drawargs *pArgs = reinterpret_cast<drawargs *>(args);
509 
512  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
513 
514  pProvider->pFramebuffer->draw(
515  pArgs->a, pArgs->b, pArgs->c, pArgs->d, pArgs->e, pArgs->f, pArgs->g,
516  static_cast<Graphics::PixelFormat>(pArgs->h));
517 }
518 
519 int pedigree_gfx_create_fbuffer(void *p, void *args)
520 {
521  if (!p || !args)
522  return -1;
523 
524  createargs *pArgs = reinterpret_cast<createargs *>(args);
525 
527  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
528  GraphicsService::GraphicsProvider *pReturnProvider =
529  reinterpret_cast<GraphicsService::GraphicsProvider *>(
530  pArgs->pReturnProvider);
531 
532  pReturnProvider->pFramebuffer = Graphics::createFramebuffer(
533  pProvider->pFramebuffer, pArgs->x, pArgs->y, pArgs->w, pArgs->h,
534  pArgs->pFramebuffer);
535  if (!pReturnProvider->pFramebuffer)
536  return -1;
537 
538  return 0;
539 }
540 
541 void pedigree_gfx_delete_fbuffer(void *p)
542 {
543  if (!p)
544  return;
545 
547  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
548 
549  Graphics::destroyFramebuffer(pProvider->pFramebuffer);
550 }
551 
552 void pedigree_gfx_fbinfo(
553  void *p, size_t *w, size_t *h, uint32_t *fmt, size_t *bypp)
554 {
555  if (!p)
556  return;
557 
559  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
560 
561  if (w)
562  *w = pProvider->pFramebuffer->getWidth();
563  if (h)
564  *h = pProvider->pFramebuffer->getHeight();
565  if (fmt)
566  *fmt = static_cast<uint32_t>(pProvider->pFramebuffer->getFormat());
567  if (bypp)
568  *bypp = pProvider->pFramebuffer->getBytesPerPixel();
569 }
570 
571 void pedigree_gfx_setpalette(void *p, uint32_t *data, size_t entries)
572 {
573  if (!p)
574  return;
575 
577  reinterpret_cast<GraphicsService::GraphicsProvider *>(p);
578 
579  pProvider->pFramebuffer->setPalette(data, entries);
580 }
581 
582 void pedigree_event_return()
583 {
584  // Return to the old code
585  Processor::information().getScheduler().eventHandlerReturned();
586 
587  FATAL("event_return: should never get here");
588 }
589 
590 void *pedigree_sys_request_mem(size_t len)
591 {
592  Process *pProcess =
593  Processor::information().getCurrentThread()->getParent();
594  uintptr_t mapAddress = 0;
595  if (!pProcess->getDynamicSpaceAllocator().allocate(len, mapAddress))
596  {
597  if (!pProcess->getSpaceAllocator().allocate(len, mapAddress))
598  {
599  return 0;
600  }
601  }
602 
603  return reinterpret_cast<void *>(mapAddress);
604 }
605 
606 void pedigree_haltfs()
607 {
608  // Synchronises all filesystems and disks and unloads them.
610  NOTICE("Stubbed: pedigree_haltfs");
611 }
File * find(const String &path, File *pStartNode=0)
Definition: VFS.cc:243
size_t cols()
Returns the number of columns.
void inhibitEvent(size_t eventNumber, bool bInhibit)
Definition: Thread.cc:571
size_t rows()
Returns the number of rows.
virtual void copy(size_t srcx, size_t srcy, size_t destx, size_t desty, size_t w, size_t h, bool bLowestCall=true)
Definition: Framebuffer.cc:190
virtual bool getCurrentScreenMode(ScreenMode &sm)
Definition: Display.cc:100
virtual void blit(Graphics::Buffer *pBuffer, size_t srcx, size_t srcy, size_t destx, size_t desty, size_t width, size_t height, bool bLowestCall=true)
Definition: Framebuffer.cc:156
virtual void getMapping(void *virtualAddress, physical_uintptr_t &physicalAddress, size_t &flags)=0
std::string getColumnName(size_t col, size_t buffSz=256)
Returns the name of the col&#39;th column.
Module * loadModule(uint8_t *pModule, size_t len, bool silent=false)
Definition: KernelElf.cc:432
virtual bool isMapped(void *virtualAddress)=0
void installCallback(CallbackType filter, callback_t callback, void *meta=0, Thread *pThread=0, uintptr_t param=0)
Installs a callback.
virtual void destroyBuffer(Graphics::Buffer *pBuffer)
Definition: Framebuffer.cc:104
static KeymapManager & instance()
Singleton design.
Definition: KeymapManager.h:41
Definition: String.h:49
virtual void * getRawBuffer() const
Definition: Framebuffer.cc:87
std::string getStr(size_t row, size_t col, size_t buffSz=256)
Returns the value in column &#39;col&#39; of the result, in string form.
static ProcessorInformation & information()
Definition: Processor.cc:45
virtual bool provides(Type service)
static VFS & instance()
Definition: VFS.cc:56
bool allocate(T length, T &address)
Definition: RangeList.h:222
static KernelElf & instance()
Definition: KernelElf.h:129
void unmap(MemoryMappedObject *pObj)
virtual void rect(size_t x, size_t y, size_t width, size_t height, uint32_t colour, Graphics::PixelFormat format=Graphics::Bits32_Argb, bool bLowestCall=true)
Definition: Framebuffer.cc:179
virtual bool isSymlink()
Definition: File.cc:431
void removeCallback(callback_t callback, void *meta=0, Thread *pThread=0)
Removes a callback.
void redraw(size_t x=~0UL, size_t y=~0UL, size_t w=~0UL, size_t h=~0UL, bool bChild=false)
Definition: Framebuffer.cc:112
size_t getNum(size_t row, size_t col)
Returns the value in column &#39;col&#39; of the result, in number form.
Memory-mapped file interface.
Result * query(const char *sql)
MemoryAllocator & getDynamicSpaceAllocator()
Definition: Process.h:183
#define NOTICE(text)
Definition: Log.h:74
static MemoryMapManager & instance()
virtual void draw(void *pBuffer, size_t srcx, size_t srcy, size_t destx, size_t desty, size_t width, size_t height, Graphics::PixelFormat format=Graphics::Bits32_Argb, bool bLowestCall=true)
Definition: Framebuffer.cc:168
MemoryMappedObject * mapFile(File *pFile, uintptr_t &address, size_t length, MemoryMappedObject::Permissions perms, size_t offset=0, bool bCopyOnWrite=true)
void setPixel(size_t x, size_t y, uint32_t colour, Graphics::PixelFormat format=Graphics::Bits32_Argb, bool bLowestCall=true)
Definition: Framebuffer.cc:214
Service * getService(const String &serviceName)
void unloadModule(const char *name, bool silent=false, bool progress=true)
Definition: KernelElf.cc:665
bool getBool(size_t row, size_t col)
Returns the value in column &#39;col&#39; of the result, in boolean form.
static InputManager & instance()
Singleton design.
Definition: InputManager.h:107
virtual bool serve(ServiceFeatures::Type type, void *pData, size_t dataLen)=0
Definition: Thread.h:54
void setPalette(uint32_t *palette, size_t nEntries)
Definition: Framebuffer.cc:71
char * getDependingModule(char *name)
Definition: KernelElf.cc:809
#define ERROR(text)
Definition: Log.h:82
virtual bool isDirectory()
Definition: File.cc:436
#define FATAL(text)
Definition: Log.h:89
virtual void line(size_t x1, size_t y1, size_t x2, size_t y2, uint32_t colour, Graphics::PixelFormat format=Graphics::Bits32_Argb, bool bLowestCall=true)
Definition: Framebuffer.cc:202
Definition: File.h:66
ServiceFeatures * enumerateOperations(const String &serviceName)
virtual Graphics::Buffer * createBuffer(const void *srcData, Graphics::PixelFormat srcFormat, size_t width, size_t height, uint32_t *pPalette=0)
Definition: Framebuffer.cc:94
MemoryAllocator & getSpaceAllocator()
Definition: Process.h:178