Subversion Repositories Kolibri OS

Rev

Rev 9827 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef __SYSCALL_H__
  2. #define __SYSCALL_H__
  3.  
  4. #include <ddk.h>
  5.  
  6. #define ASSERT_CONCAT_(a, b) a##b
  7. #define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
  8. #define STATIC_ASSERT(e) enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }
  9.  
  10. typedef u32 addr_t;
  11. typedef u32 count_t;
  12.  
  13. #pragma pack(push, 1)
  14.  
  15. typedef struct {
  16.     int width;
  17.     int height;
  18.     int bpp;
  19.     int freq;
  20. } videomode_t;
  21.  
  22. struct kos32_pdev {
  23.     struct kos32_pdev *prev;
  24.     struct kos32_pdev *next;
  25.     u32 devid;
  26.     u32 class;
  27.     u8  devfn;
  28.     u8  bus;
  29.     u8  reserved[2];
  30.     u32 owner;
  31. };
  32.  
  33. // struct BOX in const.inc
  34. struct kos_wnd_box {
  35.     u32 left;
  36.     u32 top;
  37.     u32 width;
  38.     u32 height;
  39. };
  40.  
  41. // struct DBG_REGS in const.inc
  42. struct kos_dbg_regs {
  43.     u32 dr0;
  44.     u32 dr1;
  45.     u32 dr2;
  46.     u32 dr3;
  47.     u32 dr7;
  48. };
  49.  
  50. struct kos_lhead {
  51.   struct kos_lhead *prev;
  52.   struct kos_lhead *next;
  53. };
  54.  
  55. enum KOS_SLOT_STATES {
  56.     KOS_SLOT_STATE_RUNNING = 0,
  57.     KOS_SLOT_STATE_SUSPENDED = 1,
  58.     KOS_SLOT_STATE_SUSPENDED_WAIT_EVENT = 2,
  59.     KOS_SLOT_STATE_NORMAL_TERM = 3,
  60.     KOS_SLOT_STATE_EXCEPT_TERM = 4,
  61.     KOS_SLOT_STATE_WAIT_EVENT = 5,
  62.     KOS_SLOT_STATE_FREE = 9
  63. };
  64.  
  65. struct kos_appdata {
  66.     char app_name[16];
  67.     struct kos_lhead list;
  68.     u32 process;
  69.     u32 fpu_state;
  70.     u32 exc_handler;
  71.     u32 except_mask;
  72.     u32 pl0_stack;
  73.     u32 cursor;
  74.     u32 fd_ev;
  75.     u32 bk_ev;
  76.     u32 fd_obj;
  77.     u32 bk_obj;
  78.     u32 saved_esp;
  79.     u32 io_map[2];
  80.     u32 dbg_state;
  81.     char* cur_dir;
  82.     u32 wait_timeout;
  83.     u32 saved_esp0;
  84.     u32 wait_begin;
  85.     u32 wait_test;
  86.     u32 wait_param;
  87.     u32 tls_base;
  88.     u32 event_mask;
  89.     u32 tid;
  90.     u32 draw_bgr_x;
  91.     u32 draw_bgr_y;
  92.     u8  state;
  93.     u8  wnd_number;
  94.     u16 __reserved1;
  95.     u32 wnd_shape;
  96.     u32 wnd_shape_scale;
  97.     u32 __reserved2;
  98.     u32 counter_sum;
  99.     struct kos_wnd_box saved_box;
  100.     u32 ipc_start;
  101.     u32 ipc_size;
  102.     u32 occurred_events;
  103.     u32 debugger_slot;
  104.     u32 terminate_protection;
  105.     u8  keyboard_mode;
  106.     u8  captionEncoding;
  107.     u8  __reserved3[2];
  108.     char* exec_params;
  109.     u32 dbg_event_mem;
  110.     struct kos_dbg_regs dbg_regs;
  111.     char* wnd_caption;
  112.     struct kos_wnd_box wnd_clientbox;
  113.     u32 priority;
  114.     struct kos_lhead in_schedule;
  115.     u32 counter_add;
  116.     u32 cpu_usage;
  117.     u32 __reserved4;
  118. };
  119.  
  120. #define KOS_APPDATA_SIZE 256
  121. STATIC_ASSERT(sizeof(struct kos_appdata) == KOS_APPDATA_SIZE);
  122.  
  123. typedef struct {
  124.     unsigned            p00;
  125.     union{
  126.         uint64_t        p04;
  127.         struct {
  128.             unsigned    p04dw;
  129.             unsigned    p08dw;
  130.         };
  131.     };
  132.     unsigned            p12;
  133.     union {
  134.         unsigned        p16;
  135.         const char     *new_name;
  136.         void           *bdfe;
  137.         void           *buf16;
  138.         const void     *cbuf16;
  139.     };
  140.     char                p20;
  141.     const char         *p21;
  142. } ksys70_t;
  143.  
  144. #pragma pack(pop)
  145.  
  146. ///////////////////////////////////////////////////////////////////////////////
  147.  
  148. #define STDCALL  __attribute__ ((stdcall)) __attribute__ ((dllimport))
  149. #define FASTCALL __attribute__ ((fastcall)) __attribute__ ((dllimport))
  150.  
  151. #define IMPORT   __attribute__ ((dllimport))
  152.  
  153. ///////////////////////////////////////////////////////////////////////////////
  154.  
  155. #define SysMsgBoardStr  __SysMsgBoardStr
  156. #define PciApi          __PciApi
  157. #define CreateObject    __CreateObject
  158. #define DestroyObject   __DestroyObject
  159.  
  160. #define _alloca(x) __builtin_alloca((x))
  161.  
  162. ///////////////////////////////////////////////////////////////////////////////
  163.  
  164.  
  165. void*  STDCALL AllocKernelSpace(size_t size)__asm__("AllocKernelSpace");
  166. void   STDCALL FreeKernelSpace(void *mem)__asm__("FreeKernelSpace");
  167. addr_t STDCALL MapIoMem(addr_t base, size_t size, u32 flags)__asm__("MapIoMem");
  168. void*  STDCALL KernelAlloc(size_t size)__asm__("KernelAlloc");
  169. void*  STDCALL KernelFree(const void *mem)__asm__("KernelFree");
  170. void*  STDCALL UserAlloc(size_t size)__asm__("UserAlloc");
  171. int    STDCALL UserFree(void *mem)__asm__("UserFree");
  172.  
  173. void*  STDCALL GetDisplay(void)__asm__("GetDisplay");
  174.  
  175. u32  IMPORT  GetTimerTicks(void)__asm__("GetTimerTicks");
  176. u64  IMPORT  GetClockNs(void)__asm__("GetClockNs");
  177.  
  178. addr_t STDCALL AllocPage(void)__asm__("AllocPage");
  179. addr_t STDCALL AllocPages(count_t count)__asm__("AllocPages");
  180. void   IMPORT  __attribute__((regparm(1)))
  181.                FreePage(addr_t page)__asm__("FreePage");
  182. void   STDCALL MapPage(void *vaddr, addr_t paddr, u32 flags)__asm__("MapPage");
  183.  
  184.  
  185. void* STDCALL CreateRingBuffer(size_t size, u32 map)__asm__("CreateRingBuffer");
  186.  
  187. u32 STDCALL RegService(char *name, srv_proc_t proc)__asm__("RegService");
  188.  
  189. int   STDCALL AttachIntHandler(int irq, void *handler, u32 access) __asm__("AttachIntHandler");
  190.  
  191. void  FASTCALL MutexInit(struct mutex*)__asm__("MutexInit");
  192. void  FASTCALL MutexLock(struct mutex*)__asm__("MutexLock");
  193. void  FASTCALL MutexUnlock(struct mutex*)__asm__("MutexUnlock");
  194.  
  195. void  FASTCALL InitRwsem(struct rw_semaphore *sem)__asm__("InitRwsem");
  196. void  FASTCALL DownRead(struct rw_semaphore *sem)__asm__("DownRead");
  197. void  FASTCALL DownWrite(struct rw_semaphore *sem)__asm__("DownWrite");
  198. void  FASTCALL UpRead(struct rw_semaphore *sem)__asm__("UpRead");
  199. void  FASTCALL UpWrite(struct rw_semaphore *sem)__asm__("UpWrite");
  200.  
  201. addr_t IMPORT  GetStackBase(void)__asm__("GetStackBase");
  202. u32  IMPORT  GetPid(void)__asm__("GetPid");
  203.  
  204. u32 STDCALL TimerHS(u32 delay, u32 interval,
  205.                     void *fn, void *data)asm("TimerHS");
  206.  
  207. void STDCALL CancelTimerHS(u32 handle)asm("CancelTimerHS");
  208.  
  209. u64 IMPORT GetCpuFreq()__asm__("GetCpuFreq");
  210.  
  211. ///////////////////////////////////////////////////////////////////////////////
  212.  
  213. void   STDCALL SetMouseData(int btn, int x, int y,
  214.                             int z, int h)__asm__("SetMouseData");
  215.  
  216. void   FASTCALL SetKeyboardData(u32 data)__asm__("SetKeyboardData");
  217.  
  218. struct kos32_pdev* IMPORT GetPCIList()__asm__("GetPCIList");
  219.  
  220. u8  STDCALL PciRead8 (u32 bus, u32 devfn, u32 reg)__asm__("PciRead8");
  221. u16 STDCALL PciRead16(u32 bus, u32 devfn, u32 reg)__asm__("PciRead16");
  222. u32 STDCALL PciRead32(u32 bus, u32 devfn, u32 reg)__asm__("PciRead32");
  223.  
  224. u32 STDCALL PciWrite8 (u32 bus, u32 devfn, u32 reg,u8 val) __asm__("PciWrite8");
  225. u32 STDCALL PciWrite16(u32 bus, u32 devfn, u32 reg,u16 val)__asm__("PciWrite16");
  226. u32 STDCALL PciWrite32(u32 bus, u32 devfn, u32 reg,u32 val)__asm__("PciWrite32");
  227.  
  228. struct kos_appdata* IMPORT GetCurrSlot(void) __asm__("GetCurrSlot");
  229.  
  230. #define pciReadByte(tag, reg) \
  231.         PciRead8(PCI_BUS_FROM_TAG(tag),PCI_DFN_FROM_TAG(tag),(reg))
  232.  
  233. #define pciReadWord(tag, reg) \
  234.         PciRead16(PCI_BUS_FROM_TAG(tag),PCI_DFN_FROM_TAG(tag),(reg))
  235.  
  236. #define pciReadLong(tag, reg) \
  237.         PciRead32(PCI_BUS_FROM_TAG(tag),PCI_DFN_FROM_TAG(tag),(reg))
  238.  
  239. #define pciWriteByte(tag, reg, val) \
  240.         PciWrite8(PCI_BUS_FROM_TAG(tag),PCI_DFN_FROM_TAG(tag),(reg),(val))
  241.  
  242. #define pciWriteWord(tag, reg, val) \
  243.         PciWrite16(PCI_BUS_FROM_TAG(tag),PCI_DFN_FROM_TAG(tag),(reg),(val))
  244.  
  245. #define pciWriteLong(tag, reg, val) \
  246.         PciWrite32(PCI_BUS_FROM_TAG(tag),PCI_DFN_FROM_TAG(tag),(reg),(val))
  247.  
  248.  
  249. ///////////////////////////////////////////////////////////////////////////////
  250.  
  251. int dbg_open(char *path);
  252. int dbgprintf(const char* format, ...);
  253.  
  254. ///////////////////////////////////////////////////////////////////////////////
  255.  
  256. static inline int CreateKernelThread(void *entry)
  257. {
  258.     int pid;
  259.      __asm__ __volatile__ (
  260.      "call *__imp__CreateThread"
  261.      :"=a"(pid)
  262.      :"b"(1),"c"(entry),"d"(0)
  263.      :"memory");
  264.      __asm__ __volatile__ ("":::"ebx","ecx","edx","esi","edi");
  265.      return pid;
  266. };
  267.  
  268.  
  269. static inline evhandle_t CreateEvent(kevent_t *ev, u32 flags)
  270. {
  271.      evhandle_t evh;
  272.  
  273.      __asm__ __volatile__ (
  274.      "call *__imp__CreateEvent"
  275.      :"=A"(evh.raw)
  276.      :"S" (ev), "c"(flags)
  277.      :"memory");
  278.      __asm__ __volatile__ ("":::"ebx","ecx","edx","esi", "edi");
  279.  
  280.      return evh;
  281. };
  282.  
  283. static inline void RaiseEvent(evhandle_t evh, u32 flags, kevent_t *ev)
  284. {
  285.      __asm__ __volatile__ (
  286.      "call *__imp__RaiseEvent"
  287.      ::"a"(evh.handle),"b"(evh.euid),"d"(flags),"S" (ev)
  288.      :"memory");
  289.      __asm__ __volatile__ ("":::"ebx","ecx","edx","esi","edi");
  290.  
  291. };
  292.  
  293. static inline void WaitEvent(evhandle_t evh)
  294. {
  295.      __asm__ __volatile__ (
  296.      "call *__imp__WaitEvent"
  297.      ::"a"(evh.handle),"b"(evh.euid));
  298.      __asm__ __volatile__ ("":::"ebx","ecx","edx","esi","edi");
  299. };
  300.  
  301. static inline int WaitEventTimeout(evhandle_t evh, int timeout)
  302. {
  303.     int retval;
  304.     __asm__ __volatile__ (
  305.     "call *__imp__WaitEventTimeout"
  306.     :"=a"(retval)
  307.     :"a"(evh.handle),"b"(evh.euid), "c"(timeout));
  308.     __asm__ __volatile__ ("":::"ebx","ecx","edx","esi","edi");
  309.     return retval;
  310. };
  311.  
  312. static inline void DestroyEvent(evhandle_t evh)
  313. {
  314.      __asm__ __volatile__ (
  315.      "call *__imp__DestroyEvent"
  316.      ::"a"(evh.handle),"b"(evh.euid));
  317.      __asm__ __volatile__ ("":::"ebx","ecx","edx","esi","edi");
  318. };
  319.  
  320. static inline u32 GetEvent(kevent_t *ev)
  321. {
  322.     u32  handle;
  323.  
  324.     __asm__ __volatile__ (
  325.     "call *__imp__GetEvent"
  326.     :"=a"(handle)
  327.     :"D"(ev)
  328.     :"memory");
  329.     __asm__ __volatile__ ("":::"ebx","ecx","edx", "esi","edi");
  330.      return handle;
  331. };
  332.  
  333.  
  334. static inline int GetScreenSize(void)
  335. {
  336.   int retval;
  337.  
  338.   asm("int $0x40"
  339.       :"=a"(retval)
  340.       :"a"(61), "b"(1));
  341.   return retval;
  342. }
  343.  
  344. static inline int GetScreenBpp(void)
  345. {
  346.   int retval;
  347.  
  348.   asm("int $0x40"
  349.       :"=a"(retval)
  350.       :"a"(61), "b"(2));
  351.   return retval;
  352. }
  353.  
  354. static inline int GetScreenPitch(void)
  355. {
  356.   int retval;
  357.  
  358.   asm("int $0x40"
  359.       :"=a"(retval)
  360.       :"a"(61), "b"(3));
  361.   return retval;
  362. }
  363.  
  364. static inline u32 GetPgAddr(void *mem)
  365. {
  366.      u32 retval;
  367.  
  368.      __asm__ __volatile__ (
  369.      "call *__imp__GetPgAddr \n\t"
  370.      :"=a" (retval)
  371.      :"a" (mem) );
  372.      return retval;
  373. };
  374.  
  375. static inline void CommitPages(void *mem, u32 page, u32 size)
  376. {
  377.      size = (size+4095) & ~4095;
  378.      __asm__ __volatile__ (
  379.      "call *__imp__CommitPages"
  380.      ::"a" (page), "b"(mem),"c"(size>>12)
  381.      :"edx" );
  382.      __asm__ __volatile__ ("":::"eax","ebx","ecx");
  383. };
  384.  
  385. static inline void UnmapPages(void *mem, size_t size)
  386. {
  387.      size = (size+4095) & ~4095;
  388.      __asm__ __volatile__ (
  389.      "call *__imp__UnmapPages"
  390.      ::"a" (mem), "c"(size>>12)
  391.      :"edx");
  392.      __asm__ __volatile__ ("":::"eax","ecx");
  393. };
  394.  
  395. static inline void usleep(u32 delay)
  396. {
  397.      if( !delay )
  398.         delay++;
  399.      delay*= 500;
  400.  
  401.      while(delay--)
  402.         __asm__ __volatile__(
  403.         "xorl %%eax, %%eax \n\t"
  404.         "cpuid \n\t"
  405.         :::"eax","ebx","ecx","edx");
  406.      };
  407.  
  408. static inline void udelay1(u32 delay)
  409. {
  410.     if(!delay) delay++;
  411.     delay*= 100;
  412.  
  413.     while(delay--)
  414.     {
  415.         __asm__ __volatile__(
  416.         "xorl %%eax, %%eax \n\t"
  417.         "cpuid"
  418.         :::"eax","ebx","ecx","edx" );
  419.     }
  420. }
  421.  
  422. static inline void msleep1(unsigned int msecs)
  423. {
  424.     msecs /= 10;
  425.     if(!msecs) msecs = 1;
  426.  
  427.      __asm__ __volatile__ (
  428.      "call *__imp__Delay"
  429.      ::"b" (msecs));
  430.      __asm__ __volatile__ (
  431.      "":::"ebx");
  432.  
  433. };
  434.  
  435. static inline void mdelay1(u32 time)
  436. {
  437.     time /= 10;
  438.     if(!time) time = 1;
  439.  
  440.      __asm__ __volatile__ (
  441.      "call *__imp__Delay"
  442.      ::"b" (time));
  443.      __asm__ __volatile__ (
  444.      "":::"ebx");
  445.  
  446. };
  447.  
  448. static inline u32 __PciApi(int cmd)
  449. {
  450.      u32 retval;
  451.  
  452.      __asm__ __volatile__ (
  453.      "call *__imp__PciApi \n\t"
  454.      "movzxb %%al, %%eax"
  455.      :"=a" (retval)
  456.      :"a" (cmd)
  457.      :"cc");
  458.  
  459.      return retval;
  460. };
  461.  
  462. static inline void* __CreateObject(u32 pid, size_t size)
  463. {
  464.      void *retval;
  465.  
  466.      __asm__ __volatile__ (
  467.      "call *__imp__CreateObject \n\t"
  468.      :"=a" (retval)
  469.      :"a" (size),"b"(pid)
  470.      :"esi","edi", "memory");
  471.      return retval;
  472. }
  473.  
  474. static inline void __DestroyObject(void *obj)
  475. {
  476.      __asm__ __volatile__ (
  477.      "call *__imp__DestroyObject \n\t"
  478.      :
  479.      :"a" (obj));
  480.      __asm__ __volatile__ (
  481.      ""
  482.      :::"eax","ebx","ecx","edx","esi","edi","cc","memory");
  483. }
  484.  
  485. static inline u32 GetService(const char *name)
  486. {
  487.     u32 handle;
  488.  
  489.     __asm__ __volatile__
  490.     (
  491.      "pushl %%eax \n\t"
  492.      "call *__imp__GetService"
  493.      :"=a" (handle)
  494.      :"a" (name)
  495.      :"ebx","ecx","edx","esi", "edi"
  496.   );
  497.   return handle;
  498. };
  499.  
  500. static inline u32 safe_cli(void)
  501. {
  502.      u32 ifl;
  503.      __asm__ __volatile__ (
  504.      "pushf\n\t"
  505.      "popl %0\n\t"
  506.      "cli\n"
  507.      : "=r" (ifl));
  508.     return ifl;
  509. }
  510.  
  511. static inline void safe_sti(u32 efl)
  512. {
  513.      if (efl & (1<<9))
  514.         __asm__ __volatile__ ("sti");
  515. }
  516.  
  517. static inline u32 get_eflags(void)
  518. {
  519.     u32 val;
  520.     asm volatile (
  521.     "pushfl\n\t"
  522.     "popl %0\n"
  523.     : "=r" (val));
  524.     return val;
  525. }
  526.  
  527. static inline void __clear (void * dst, unsigned len)
  528. {
  529.      u32 tmp;
  530.      __asm__ __volatile__ (
  531.      "cld \n\t"
  532.      "rep stosb \n"
  533.      :"=c"(tmp),"=D"(tmp)
  534.      :"a"(0),"c"(len),"D"(dst));
  535.      __asm__ __volatile__ ("":::"ecx","edi");
  536. };
  537.  
  538. static inline void out8(const u16 port, const u8 val)
  539. {
  540.     __asm__ __volatile__
  541.     ("outb  %1, %0\n" : : "dN"(port), "a"(val));
  542. }
  543.  
  544. static inline void out16(const u16 port, const u16 val)
  545. {
  546.     __asm__ __volatile__
  547.     ("outw  %1, %0\n" : : "dN"(port), "a"(val));
  548. }
  549.  
  550. static inline void out32(const u16 port, const u32 val)
  551. {
  552.     __asm__ __volatile__
  553.     ("outl  %1, %0\n" : : "dN"(port), "a"(val));
  554. }
  555.  
  556. static inline u8 in8(const u16 port)
  557. {
  558.     u8 tmp;
  559.     __asm__ __volatile__
  560.     ("inb %1, %0\n" : "=a"(tmp) : "dN"(port));
  561.     return tmp;
  562. };
  563.  
  564. static inline u16 in16(const u16 port)
  565. {
  566.     u16 tmp;
  567.     __asm__ __volatile__
  568.     ("inw %1, %0\n" : "=a"(tmp) : "dN"(port));
  569.     return tmp;
  570. };
  571.  
  572. static inline u32 in32(const u16 port)
  573. {
  574.     u32 tmp;
  575.     __asm__ __volatile__
  576.     ("inl %1, %0\n" : "=a"(tmp) : "dN"(port));
  577.     return tmp;
  578. };
  579.  
  580. static inline void delay(int time)
  581. {
  582.      __asm__ __volatile__ (
  583.      "call *__imp__Delay"
  584.      ::"b" (time));
  585.      __asm__ __volatile__ (
  586.      "":::"ebx");
  587.  
  588. }
  589.  
  590. static inline void change_task(void)
  591. {
  592.      __asm__ __volatile__ (
  593.      "call *__imp__ChangeTask");
  594. }
  595.  
  596. static inline void sysSetScreen(int width, int height, int pitch)
  597. {
  598.     __asm__ __volatile__
  599.     (
  600.         "call *__imp__SetScreen"
  601.         :
  602.         :"a" (width-1),"d"(height-1), "c"(pitch)
  603.     );
  604.     __asm__ __volatile__
  605.     ("" :::"eax","ecx","edx");
  606. }
  607.  
  608. void  FASTCALL sysSetFramebuffer(void *fb)__asm__("SetFramebuffer");
  609.  
  610.  
  611. static inline void __SysMsgBoardStr(char *text)
  612. {
  613.     __asm__ __volatile__(
  614.     "call *__imp__SysMsgBoardStr"
  615.     ::"S" (text));
  616. };
  617.  
  618. static inline void *KernelZeroAlloc(unsigned long size)
  619. {
  620.     void *mem;
  621.  
  622.     mem = KernelAlloc(size);
  623.     if(mem)
  624.         memset(mem, 0, size);
  625.  
  626.    return mem;
  627. };
  628.  
  629. static inline int power_supply_is_system_supplied(void) { return -1; };
  630.  
  631. static inline int FS_Service(ksys70_t *k, int* err)
  632. {
  633.     int status;
  634.     __asm__ __volatile__(
  635.      "call *__imp__FS_Service"
  636.      :"=a" (status), "=b" (*err)
  637.      :"b" (k)
  638.      :"memory"
  639.     );
  640.     __asm__ __volatile__ ("":::"ebx","ecx","edx","esi","edi");
  641.     return status;
  642. }
  643. #endif
  644.