Subversion Repositories Kolibri OS

Rev

Rev 9926 | Rev 9932 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ;;
  3. ;; Copyright (C) KolibriOS team 2004-2022. All rights reserved.
  4. ;; PROGRAMMING:
  5. ;; Ivan Poddubny
  6. ;; Marat Zakiyanov (Mario79)
  7. ;; VaStaNi
  8. ;; Trans
  9. ;; Mihail Semenyako (mike.dld)
  10. ;; Sergey Kuzmin (Wildwest)
  11. ;; Andrey Halyavin (halyavin)
  12. ;; Mihail Lisovin (Mihasik)
  13. ;; Andrey Ignatiev (andrew_programmer)
  14. ;; NoName
  15. ;; Evgeny Grechnikov (Diamond)
  16. ;; Iliya Mihailov (Ghost)
  17. ;; Sergey Semyonov (Serge)
  18. ;; Johnny_B
  19. ;; SPraid (simba)
  20. ;; Hidnplayr
  21. ;; Alexey Teplov (<Lrz>)
  22. ;; Rus
  23. ;; Nable
  24. ;; shurf
  25. ;; Alver
  26. ;; Maxis
  27. ;; Galkov
  28. ;; CleverMouse
  29. ;; tsdima
  30. ;; turbanoff
  31. ;; Asper
  32. ;; art_zh
  33. ;; dunkaist
  34. ;; Coldy
  35. ;; rgimad
  36. ;; Boppan
  37. ;; Doczom
  38. ;; and others
  39. ;;
  40. ;; Data in this file was originally part of MenuetOS project which is
  41. ;; distributed under the terms of GNU GPL. It is modified and redistributed as
  42. ;; part of KolibriOS project under the terms of GNU GPL.
  43. ;;
  44. ;; Copyright (C) MenuetOS 2000-2004 Ville Mikael Turjanmaa
  45. ;; PROGRAMMING:
  46. ;;
  47. ;; Ville Mikael Turjanmaa, villemt@itu.jyu.fi
  48. ;; - main os coding/design
  49. ;; Jan-Michael Brummer, BUZZ2@gmx.de
  50. ;; Felix Kaiser, info@felix-kaiser.de
  51. ;; Paolo Minazzi, paolo.minazzi@inwind.it
  52. ;; quickcode@mail.ru
  53. ;; Alexey, kgaz@crosswinds.net
  54. ;; Juan M. Caravaca, bitrider@wanadoo.es
  55. ;; kristol@nic.fi
  56. ;; Mike Hibbett, mikeh@oceanfree.net
  57. ;; Lasse Kuusijarvi, kuusijar@lut.fi
  58. ;; Jarek Pelczar, jarekp3@wp.pl
  59. ;;
  60. ;; KolibriOS is distributed in the hope that it will be useful, but WITHOUT ANY
  61. ;; WARRANTY. No author or distributor accepts responsibility to anyone for the
  62. ;; consequences of using it or for whether it serves any particular purpose or
  63. ;; works at all, unless he says so in writing. Refer to the GNU General Public
  64. ;; License (the "GPL") for full details.
  65. ;
  66. ;; Everyone is granted permission to copy, modify and redistribute KolibriOS,
  67. ;; but only under the conditions described in the GPL. A copy of this license
  68. ;; is supposed to have been given to you along with KolibriOS so you can know
  69. ;; your rights and responsibilities. It should be in a file named COPYING.
  70. ;; Among other things, the copyright notice and this notice must be preserved
  71. ;; on all copies.
  72. ;;
  73. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  74.  
  75. format binary as "mnt"
  76.  
  77. include 'macros.inc'
  78. include 'struct.inc'
  79.  
  80. $Revision: 9930 $
  81.  
  82.  
  83. USE_COM_IRQ     = 1      ; make irq 3 and irq 4 available for PCI devices
  84. VESA_1_2_VIDEO  = 0      ; enable vesa 1.2 bank switch functions
  85.  
  86. ; Enabling the next line will enable serial output console
  87. ;debug_com_base  = 0x3f8  ; 0x3f8 is com1, 0x2f8 is com2, 0x3e8 is com3, 0x2e8 is com4, no irq's are used
  88.  
  89. include "proc32.inc"
  90. include "kglobals.inc"
  91. include "lang.inc"
  92. include "encoding.inc"
  93.  
  94. include "const.inc"
  95.  
  96. iglobal
  97. ; The following variable, if equal to 1, duplicates debug output to the screen.
  98. debug_direct_print db 0
  99. ; Start the first app (LAUNCHER) after kernel is loaded? (1=yes, 2 or 0=no)
  100. launcher_start db 1
  101. endg
  102.  
  103. max_processes  =  255
  104. tss_step       =  128 + 8192 ; tss & i/o - 65535 ports, * 256=557056*4
  105.  
  106. os_stack       =  os_data_l - gdts    ; GDTs
  107. os_code        =  os_code_l - gdts
  108. graph_data     =  3 + graph_data_l - gdts
  109. tss0           =  tss0_l - gdts
  110. app_code       =  3 + app_code_l - gdts
  111. app_data       =  3 + app_data_l - gdts
  112. app_tls        =  3 + tls_data_l - gdts
  113. pci_code_sel   =  pci_code_32-gdts
  114. pci_data_sel   =  pci_data_32-gdts
  115.  
  116.  
  117. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  118. ;;
  119. ;;   Included files:
  120. ;;
  121. ;;   Kernel16.inc
  122. ;;    - Booteng.inc   English text for bootup
  123. ;;    - Bootcode.inc  Hardware setup
  124. ;;    - Pci16.inc     PCI functions
  125. ;;
  126. ;;   Kernel32.inc
  127. ;;    - Sys32.inc     Process management
  128. ;;    - Shutdown.inc  Shutdown and restart
  129. ;;    - Fat32.inc     Read / write hd
  130. ;;    - Vesa12.inc    Vesa 1.2 driver
  131. ;;    - Vesa20.inc    Vesa 2.0 driver
  132. ;;    - Vga.inc       VGA driver
  133. ;;    - Stack.inc     Network interface
  134. ;;    - Mouse.inc     Mouse pointer
  135. ;;    - Scincode.inc  Window skinning
  136. ;;    - Pci32.inc     PCI functions
  137. ;;
  138. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  139.  
  140. ; In bios boot mode the kernel code below is appended to bootbios.bin file.
  141. ; That is a loading and initialization code that also draws the blue screen
  142. ; menu with svn revision number near top right corner of the screen. This fasm
  143. ; preprocessor code searches for '****' signature inside bootbios.bin and
  144. ; places revision number there.
  145. if ~ defined UEFI
  146.   bootbios:
  147.   if defined extended_primary_loader
  148.     file 'bootbios.bin.ext_loader'
  149.   else if defined pretest_build
  150.     file 'bootbios.bin.pretest'
  151.   else
  152.     file 'bootbios.bin'
  153.   end if
  154.   if __REV__ > 0
  155.     cur_pos = 0
  156.     cnt = 0
  157.     repeat $ - bootbios
  158.       load a byte from %
  159.       if a = '*'
  160.         cnt = cnt + 1
  161.       else
  162.         cnt = 0
  163.       end if
  164.       if cnt = 4
  165.         cur_pos = % - 1
  166.         break
  167.       end if
  168.     end repeat
  169.     store byte ' ' at cur_pos + 1
  170.     rev_var = __REV__
  171.     while rev_var > 0
  172.       store byte rev_var mod 10 + '0' at cur_pos
  173.       cur_pos = cur_pos - 1
  174.       rev_var = rev_var / 10
  175.     end while
  176.       store byte ' ' at cur_pos
  177.       store dword ' SVN' at cur_pos - 4
  178.   end if
  179. end if
  180.  
  181. use32
  182. org $+0x10000
  183.  
  184. align 4
  185. B32:
  186.         mov     ax, os_stack       ; Selector for os
  187.         mov     ds, ax
  188.         mov     es, ax
  189.         mov     fs, ax
  190.         mov     gs, ax
  191.         mov     ss, ax
  192.         mov     esp, TMP_STACK_TOP       ; Set stack
  193.  
  194. ; CLEAR 0x280000 - HEAP_BASE
  195.  
  196.         xor     eax, eax
  197.         mov     edi, CLEAN_ZONE
  198.         mov     ecx, (HEAP_BASE - OS_BASE - CLEAN_ZONE) / 4
  199.         cld
  200.         rep stosd
  201.  
  202. ; CLEAR KERNEL UNDEFINED GLOBALS
  203.         mov     edi, endofcode - OS_BASE
  204.         mov     ecx, 0x90000
  205.         sub     ecx, edi
  206.         shr     ecx, 2
  207.         rep stosd
  208.  
  209. ; SAVE & CLEAR 0-0xffff
  210.  
  211.         mov     edi, 0x1000
  212.         mov     ecx, 0x8000 / 4
  213.         rep stosd
  214.         mov     edi, 0xa000
  215.         mov     ecx, 0x6000 / 4
  216.         rep stosd
  217.  
  218.         call    test_cpu
  219.         bts     [cpu_caps - OS_BASE], CAPS_TSC    ;force use rdtsc
  220.  
  221.         call    acpi_locate
  222.         call    init_BIOS32
  223. ; MEMORY MODEL
  224.         call    mem_test
  225.         call    init_mem
  226.         call    init_page_map
  227.  
  228. ; ENABLE PAGING
  229.  
  230.         mov     eax, sys_proc - OS_BASE + PROC.pdt_0
  231.         mov     cr3, eax
  232.  
  233.         mov     eax, cr0
  234.         or      eax, CR0_PG + CR0_WP
  235.         mov     cr0, eax
  236.  
  237.         lgdt    [gdts]
  238.         jmp     pword os_code:high_code
  239.  
  240. align 4
  241. bios32_entry    dd ?
  242. tmp_page_tabs   dd ?
  243. use16
  244. ap_init16:
  245.         cli
  246.         lgdt    [cs:gdts_ap - ap_init16]
  247.         mov     eax, [cs:cr3_ap - ap_init16]
  248.         mov     cr3, eax
  249.         mov     eax, [cs:cr4_ap - ap_init16]
  250.         mov     cr4, eax
  251.         mov     eax, CR0_PE + CR0_PG + CR0_WP
  252.         mov     cr0, eax
  253.         jmp     pword os_code:ap_init_high
  254. align 16
  255. gdts_ap:
  256.         dw     gdte-gdts-1
  257.         dd     gdts
  258.         dw     0
  259. cr3_ap  dd     ?
  260. cr4_ap  dd     ?
  261. ap_init16_size = $ - ap_init16
  262. use32
  263.  
  264. __DEBUG__ fix 1
  265. __DEBUG_LEVEL__ fix 1
  266. include 'init.inc'
  267.  
  268. org OS_BASE+$
  269.  
  270. include 'fdo.inc'
  271.  
  272. align 4
  273. high_code:
  274.         mov     ax, os_stack
  275.         mov     bx, app_data
  276.         mov     cx, app_tls
  277.         mov     ss, ax
  278.         add     esp, OS_BASE
  279.  
  280.         mov     ds, bx
  281.         mov     es, bx
  282.         mov     fs, cx
  283.         mov     gs, bx
  284.  
  285.         xor     eax, eax
  286.         mov     ebx, 0xFFFFF000 + PG_SHARED + PG_NOCACHE + PG_UWR
  287.         bt      [cpu_caps], CAPS_PAT
  288.         setc    al
  289.         shl     eax, 7
  290.         or      ebx, eax
  291.  
  292.         mov     eax, PG_GLOBAL
  293.         bt      [cpu_caps], CAPS_PGE
  294.         jnc     @F
  295.  
  296.         or      [sys_proc + PROC.pdt_0 + (OS_BASE shr 20)], eax
  297.         or      ebx, eax
  298.  
  299.         mov     eax, cr4
  300.         or      eax, CR4_PGE
  301.         mov     cr4, eax
  302. @@:
  303.         mov     [pte_valid_mask], ebx
  304.  
  305.         xor     eax, eax
  306.         mov     dword [sys_proc + PROC.pdt_0], eax
  307.         mov     dword [sys_proc + PROC.pdt_0+4], eax
  308.  
  309.         mov     eax, cr3
  310.         mov     cr3, eax          ; flush TLB
  311.  
  312.         mov     ecx, pg_data.mutex
  313.         call    mutex_init
  314.  
  315.         mov     ecx, disk_list_mutex
  316.         call    mutex_init
  317.  
  318.         mov     ecx, keyboard_list_mutex
  319.         call    mutex_init
  320.  
  321.         mov     ecx, unpack_mutex
  322.         call    mutex_init
  323.  
  324.         mov     ecx, application_table_mutex
  325.         call    mutex_init
  326.  
  327.         mov     ecx, ide_mutex
  328.         call    mutex_init
  329.         mov     ecx, ide_channel1_mutex
  330.         call    mutex_init
  331.         mov     ecx, ide_channel2_mutex
  332.         call    mutex_init
  333.         mov     ecx, ide_channel3_mutex
  334.         call    mutex_init
  335.         mov     ecx, ide_channel4_mutex
  336.         call    mutex_init
  337.         mov     ecx, ide_channel5_mutex
  338.         call    mutex_init
  339.         mov     ecx, ide_channel6_mutex
  340.         call    mutex_init
  341. ;-----------------------------------------------------------------------------
  342. ; SAVE REAL MODE VARIABLES
  343. ;-----------------------------------------------------------------------------
  344. ; --------------- APM ---------------------
  345.  
  346. ; init selectors
  347.         mov     ebx, [BOOT.apm_entry]        ; offset of APM entry point
  348.         movzx   eax, word [BOOT.apm_code_32] ; real-mode segment base address of
  349.                                              ; protected-mode 32-bit code segment
  350.         movzx   ecx, word [BOOT.apm_code_16] ; real-mode segment base address of
  351.                                              ; protected-mode 16-bit code segment
  352.         movzx   edx, word [BOOT.apm_data_16] ; real-mode segment base address of
  353.                                              ; protected-mode 16-bit data segment
  354.  
  355.         shl     eax, 4
  356.         mov     [dword apm_code_32 + 2], ax
  357.         shr     eax, 16
  358.         mov     [dword apm_code_32 + 4], al
  359.  
  360.         shl     ecx, 4
  361.         mov     [dword apm_code_16 + 2], cx
  362.         shr     ecx, 16
  363.         mov     [dword apm_code_16 + 4], cl
  364.  
  365.         shl     edx, 4
  366.         mov     [dword apm_data_16 + 2], dx
  367.         shr     edx, 16
  368.         mov     [dword apm_data_16 + 4], dl
  369.  
  370.         mov     dword[apm_entry], ebx
  371.         mov     word [apm_entry + 4], apm_code_32 - gdts
  372.  
  373.         mov     eax, dword[BOOT.apm_version]   ; version & flags
  374.         mov     [apm_vf], eax
  375. ; -----------------------------------------
  376.         mov     al, [BOOT.dma]            ; DMA access
  377.         mov     [allow_dma_access], al
  378.  
  379.         mov     al, [BOOT.debug_print]    ; If nonzero, duplicates debug output to the screen
  380.         mov     [debug_direct_print], al
  381.  
  382.         mov     al, [BOOT.launcher_start] ; Start the first app (LAUNCHER) after kernel is loaded?
  383.         mov     [launcher_start], al
  384.  
  385.         mov     eax, [BOOT.devicesdat_size]
  386.         mov     [acpi_dev_size], eax
  387.         mov     eax, [BOOT.devicesdat_data]
  388.         mov     [acpi_dev_data], eax
  389.  
  390.         mov     esi, BOOT.bios_hd
  391.         movzx   ecx, byte [esi-1]
  392.         mov     [NumBiosDisks], ecx
  393.         mov     edi, BiosDisksData
  394.         shl     ecx, 2
  395.         rep movsd
  396.  
  397. ; -------- Fast System Call init ----------
  398. ; Intel SYSENTER/SYSEXIT (AMD CPU support it too)
  399.         bt      [cpu_caps], CAPS_SEP
  400.         jnc     .SEnP  ; SysEnter not Present
  401.         xor     edx, edx
  402.         mov     ecx, MSR_SYSENTER_CS
  403.         mov     eax, os_code
  404.         wrmsr
  405.         mov     ecx, MSR_SYSENTER_ESP
  406. ;           mov eax, sysenter_stack ; Check it
  407.         xor     eax, eax
  408.         wrmsr
  409.         mov     ecx, MSR_SYSENTER_EIP
  410.         mov     eax, sysenter_entry
  411.         wrmsr
  412. .SEnP:
  413. ; AMD SYSCALL/SYSRET
  414.         cmp     byte[cpu_vendor], 'A'
  415.         jne     .noSYSCALL
  416.         mov     eax, 0x80000001
  417.         cpuid
  418.         test    edx, 0x800  ; bit_11 - SYSCALL/SYSRET support
  419.         jz      .noSYSCALL
  420.         mov     ecx, MSR_AMD_EFER
  421.         rdmsr
  422.         or      eax, 1 ; bit_0 - System Call Extension (SCE)
  423.         wrmsr
  424.  
  425.         ; !!!! It`s dirty hack, fix it !!!
  426.         ; Bits of EDX :
  427.         ; Bit 31–16 During the SYSRET instruction, this field is copied into the CS register
  428.         ;  and the contents of this field, plus 8, are copied into the SS register.
  429.         ; Bit 15–0 During the SYSCALL instruction, this field is copied into the CS register
  430.         ;  and the contents of this field, plus 8, are copied into the SS register.
  431.  
  432.         ; mov   edx, (os_code + 16) * 65536 + os_code
  433.         mov     edx, 0x1B0008
  434.  
  435.         mov     eax, syscall_entry
  436.         mov     ecx, MSR_AMD_STAR
  437.         wrmsr
  438. .noSYSCALL:
  439. ; -----------------------------------------
  440.         stdcall alloc_page
  441.         stdcall map_page, tss-0xF80, eax, PG_SWR
  442.         stdcall alloc_page
  443.         stdcall map_page, tss+0x80, eax, PG_SWR
  444.         stdcall alloc_page
  445.         stdcall map_page, tss+0x1080, eax, PG_SWR
  446.  
  447. ; LOAD IDT
  448.  
  449.         call    build_interrupt_table ;lidt is executed
  450.           ;lidt [idtreg]
  451.  
  452.         call    init_kernel_heap
  453.         call    init_fpu
  454.         mov     eax, [xsave_area_size]
  455.         lea     eax, [eax*2 + RING0_STACK_SIZE*2]
  456.         stdcall kernel_alloc, eax
  457.         mov     [os_stack_seg], eax
  458.  
  459.         lea     esp, [eax + RING0_STACK_SIZE]
  460.  
  461.         mov     [tss._ss0], os_stack
  462.         mov     [tss._esp0], esp
  463.         mov     [tss._esp], esp
  464.         mov     [tss._cs], os_code
  465.         mov     [tss._ss], os_stack
  466.         mov     [tss._ds], app_data
  467.         mov     [tss._es], app_data
  468.         mov     [tss._fs], app_data
  469.         mov     [tss._gs], app_data
  470.         mov     [tss._io], 128
  471. ;Add IO access table - bit array of permitted ports
  472.         mov     edi, tss._io_map_0
  473.         xor     eax, eax
  474.         not     eax
  475.         mov     ecx, 8192/4
  476.         rep stosd                    ; access to 4096*8=65536 ports
  477.  
  478.         mov     ax, tss0
  479.         ltr     ax
  480.  
  481.         mov     eax, sys_proc
  482.         list_init eax
  483.         add     eax, PROC.thr_list
  484.         list_init eax
  485.  
  486.         call    init_video
  487.         call    init_mtrr
  488.         mov     [LFBAddress], LFB_BASE
  489.         mov     ecx, bios_fb
  490.         call    set_framebuffer
  491.         call    init_malloc
  492.  
  493.         stdcall alloc_kernel_space, 0x50000         ; FIXME check size
  494.         mov     [default_io_map], eax
  495.  
  496.         add     eax, 0x2000
  497.         mov     [ipc_tmp], eax
  498.         mov     ebx, 0x1000
  499.  
  500.         add     eax, 0x40000
  501.         mov     [proc_mem_map], eax
  502.  
  503.         add     eax, 0x8000
  504.         mov     [proc_mem_pdir], eax
  505.  
  506.         add     eax, ebx
  507.         mov     [proc_mem_tab], eax
  508.  
  509.         add     eax, ebx
  510.         mov     [tmp_task_ptab], eax
  511.  
  512.         add     eax, ebx
  513.         mov     [ipc_pdir], eax
  514.  
  515.         add     eax, ebx
  516.         mov     [ipc_ptab], eax
  517.  
  518.         stdcall kernel_alloc, (unpack.LZMA_BASE_SIZE + (unpack.LZMA_LIT_SIZE shl \
  519.                 (unpack.lc + unpack.lp)))*4
  520.  
  521.         mov     [unpack.p], eax
  522.  
  523.         call    init_events
  524.         mov     eax, srv.fd - SRV.fd
  525.         mov     [srv.fd], eax
  526.         mov     [srv.bk], eax
  527.  
  528. ;Set base of graphic segment to linear address of LFB
  529.         mov     eax, [LFBAddress]         ; set for gs
  530.         mov     [graph_data_l+2], ax
  531.         shr     eax, 16
  532.         mov     [graph_data_l+4], al
  533.         mov     [graph_data_l+7], ah
  534.  
  535.         stdcall kernel_alloc, [_display.win_map_size]
  536.         mov     [_display.win_map], eax
  537.  
  538.         xor     eax, eax
  539.         inc     eax
  540.  
  541. ; set background
  542.  
  543.         mov     [BgrDrawMode], eax
  544.         mov     [BgrDataWidth], eax
  545.         mov     [BgrDataHeight], eax
  546.         mov     [mem_BACKGROUND], 4
  547.         mov     [img_background], static_background_data
  548.  
  549. ; set clipboard
  550.  
  551.         xor     eax, eax
  552.         mov     [clipboard_slots], eax
  553.         mov     [clipboard_write_lock], eax
  554.         stdcall kernel_alloc, PAGE_SIZE
  555.         test    eax, eax
  556.         jnz     @f
  557.  
  558.         dec     eax
  559. @@:
  560.         mov     [clipboard_main_list], eax
  561.  
  562.         call    check_acpi
  563.  
  564.         mov     eax, [hpet_base]
  565.         test    eax, eax
  566.         jz      @F
  567.         stdcall map_io_mem, [hpet_base], 1024, PG_GLOBAL + PAT_UC + PG_SWR
  568.         mov     [hpet_base], eax
  569.         mov     eax, [eax+HPET_ID]
  570.         DEBUGF  1, "K : HPET caps %x\n", eax
  571.         call    init_hpet
  572. @@:
  573. ; SET UP OS TASK
  574.  
  575.         mov     esi, boot_setostask
  576.         call    boot_log
  577.  
  578.         mov     edi, sys_proc + PROC.heap_lock
  579.         mov     ecx, (PROC.ht_free - PROC.heap_lock)/4
  580.  
  581.         xor     eax, eax
  582.         cld
  583.         rep stosd
  584.  
  585.         mov     [edi], dword (PROC.pdt_0 - PROC.htab)/4 - 3
  586.         mov     [edi+4], dword 3           ;reserve handles for stdin stdout and stderr
  587.         mov     ecx, (PROC.pdt_0 - PROC.htab)/4
  588.         add     edi, 8
  589.         inc     eax
  590. @@:
  591.         stosd
  592.         inc     eax
  593.         cmp     eax, ecx
  594.         jbe     @B
  595.  
  596.         mov     [sys_proc + PROC.pdt_0_phys], sys_proc - OS_BASE + PROC.pdt_0
  597.  
  598.         mov     eax, -1
  599.         mov     edi, thr_slot_map+4
  600.         mov     [edi-4], dword 0xFFFFFFF8
  601.         stosd
  602.         stosd
  603.         stosd
  604.         stosd
  605.         stosd
  606.         stosd
  607.         stosd
  608.  
  609.         mov     [current_process], sys_proc
  610.  
  611.         mov     edx, SLOT_BASE + sizeof.APPDATA*1
  612.         mov     ebx, [os_stack_seg]
  613.         add     ebx, RING0_STACK_SIZE
  614.         add     ebx, [xsave_area_size]
  615.         call    setup_os_slot
  616.         mov     dword [edx], 'IDLE'
  617.         sub     [edx + APPDATA.saved_esp], 4
  618.         mov     eax, [edx + APPDATA.saved_esp]
  619.         mov     dword [eax], idle_thread
  620.         mov     ecx, IDLE_PRIORITY
  621.         call    scheduler_add_thread
  622.  
  623.         mov     edx, SLOT_BASE + sizeof.APPDATA*2
  624.         mov     ebx, [os_stack_seg]
  625.         call    setup_os_slot
  626.         mov     dword [edx], 'OS'
  627.         xor     ecx, ecx
  628.         call    scheduler_add_thread
  629.  
  630.         mov     dword [current_slot_idx], 2
  631.         mov     [thread_count], 2
  632.         mov     dword [current_slot], SLOT_BASE + sizeof.APPDATA*2
  633.  
  634. ; Move other CPUs to deep sleep, if it is useful
  635. uglobal
  636. use_mwait_for_idle db 0
  637. endg
  638.         cmp     [cpu_vendor+8], 'ntel'
  639.         jnz     .no_wake_cpus
  640.         bt      [cpu_caps+4], CAPS_MONITOR-32
  641.         jnc     .no_wake_cpus
  642.         dbgstr 'using mwait for idle loop'
  643.         inc     [use_mwait_for_idle]
  644.         mov     ebx, [cpu_count]
  645.         cmp     ebx, 1
  646.         jbe     .no_wake_cpus
  647.         call    create_trampoline_pgmap
  648.         mov     [cr3_ap + OS_BASE], eax
  649.         mov     eax, cr4
  650.         mov     [cr4_ap + OS_BASE], eax
  651.         mov     esi, OS_BASE + ap_init16
  652.         mov     edi, OS_BASE + 8000h
  653.         mov     ecx, (ap_init16_size + 3) / 4
  654.         rep movsd
  655.         mov     eax, [LAPIC_BASE]
  656.         test    eax, eax
  657.         jnz     @f
  658.         stdcall map_io_mem, [acpi_lapic_base], 0x1000, PG_GLOBAL + PG_NOCACHE + PG_SWR
  659.         mov     [LAPIC_BASE], eax
  660. @@:
  661.         lea     edi, [eax + APIC_ICRL]
  662.         mov     esi, smpt+4
  663.         dec     ebx
  664. .wake_cpus_loop:
  665.         lodsd
  666.         push    esi
  667.         xor     esi, esi
  668.         inc     esi
  669.         shl     eax, 24
  670.         mov     [edi+10h], eax
  671. ; assert INIT IPI
  672.         mov     dword [edi], 0C500h
  673.         call    delay_ms
  674. @@:
  675.         test    dword [edi], 1000h
  676.         jnz     @b
  677. ; deassert INIT IPI
  678.         mov     dword [edi], 8500h
  679.         call    delay_ms
  680. @@:
  681.         test    dword [edi], 1000h
  682.         jnz     @b
  683. ; send STARTUP IPI
  684.         mov     dword [edi], 600h + (8000h shr 12)
  685.         call    delay_ms
  686. @@:
  687.         test    dword [edi], 1000h
  688.         jnz     @b
  689.         pop     esi
  690.         dec     ebx
  691.         jnz     .wake_cpus_loop
  692.         mov     eax, [cpu_count]
  693.         dec     eax
  694. @@:
  695.         cmp     [ap_initialized], eax
  696.         jnz     @b
  697.         mov     eax, [cr3_ap + OS_BASE]
  698.         call    free_page
  699. .no_wake_cpus:
  700.  
  701. ; REDIRECT ALL IRQ'S TO INT'S 0x20-0x2f
  702.         mov     esi, boot_initirq
  703.         call    boot_log
  704.         call    init_irqs
  705.  
  706.         mov     esi, boot_picinit
  707.         call    boot_log
  708.         call    PIC_init
  709.  
  710.         mov     esi, boot_v86machine
  711.         call    boot_log
  712. ; Initialize system V86 machine
  713.         call    init_sys_v86
  714.  
  715.         mov     esi, boot_inittimer
  716.         call    boot_log
  717. ; Initialize system timer (IRQ0)
  718.         call    PIT_init
  719.  
  720. ; Register ramdisk file system
  721. if ~ defined extended_primary_loader
  722.         cmp     [BOOT.rd_load_from], RD_LOAD_FROM_HD    ; will be loaded later
  723.         je      @f
  724. end if
  725.         cmp     [BOOT.rd_load_from], RD_LOAD_FROM_NONE
  726.         je      @f
  727.         call    register_ramdisk
  728. ;--------------------------------------
  729. @@:
  730.         mov     esi, boot_initapic
  731.         call    boot_log
  732. ; Try to Initialize APIC
  733.         call    APIC_init
  734.  
  735.         mov     esi, boot_enableirq
  736.         call    boot_log
  737. ; Enable timer IRQ (IRQ0) and co-processor IRQ (IRQ13)
  738. ; they are used: when partitions are scanned, hd_read relies on timer
  739.         call    unmask_timer
  740.         ; Prevent duplicate timer IRQs in APIC mode
  741.         cmp     [irq_mode], IRQ_APIC
  742.         jz      @f
  743.         stdcall enable_irq, 2               ; @#$%! PIC
  744. @@:
  745.         stdcall enable_irq, 13              ; co-processor
  746.  
  747. ; Setup serial output console (if enabled)
  748. if defined debug_com_base
  749.  
  750.         ; reserve port so nobody else will use it
  751.         xor     ebx, ebx
  752.         mov     ecx, debug_com_base
  753.         mov     edx, debug_com_base+7
  754.         call    r_f_port_area
  755.  
  756.         ; enable Divisor latch
  757.         mov     dx, debug_com_base+3
  758.         mov     al, 1 shl 7
  759.         out     dx, al
  760.  
  761.         ; Set speed to 115200 baud (max speed)
  762.         mov     dx, debug_com_base
  763.         mov     al, 0x01
  764.         out     dx, al
  765.  
  766.         mov     dx, debug_com_base+1
  767.         mov     al, 0x00
  768.         out     dx, al
  769.  
  770.         ; No parity, 8bits words, one stop bit, dlab bit back to 0
  771.         mov     dx, debug_com_base+3
  772.         mov     al, 3
  773.         out     dx, al
  774.  
  775.         ; disable interrupts
  776.         mov     dx, debug_com_base+1
  777.         mov     al, 0
  778.         out     dx, al
  779.  
  780.         ; clear +  enable fifo (64 bits)
  781.         mov     dx, debug_com_base+2
  782.         mov     al, 0x7 + 1 shl 5
  783.         out     dx, al
  784.  
  785. end if
  786.  
  787.  
  788. ;-----------------------------------------------------------------------------
  789. ; show SVN version of kernel on the message board
  790. ;-----------------------------------------------------------------------------
  791.         mov     eax, [version_inf.rev]
  792.         DEBUGF  1, "K : kernel SVN r%d\n", eax
  793. ;-----------------------------------------------------------------------------
  794. ; show CPU count on the message board
  795. ;-----------------------------------------------------------------------------
  796.         mov     eax, [cpu_count]
  797.         test    eax, eax
  798.         jnz     @F
  799.         mov     al, 1                             ; at least one CPU
  800. @@:
  801.         DEBUGF  1, "K : %d CPU detected\n", eax
  802. ;-----------------------------------------------------------------------------
  803. ; detect Floppy drives
  804. ;-----------------------------------------------------------------------------
  805.         mov     esi, boot_detectfloppy
  806.         call    boot_log
  807. include 'detect/dev_fd.inc'
  808. ;-----------------------------------------------------------------------------
  809. ; create pci-devices list
  810. ;-----------------------------------------------------------------------------
  811.         mov     [pci_access_enabled], 1
  812.         call    pci_enum
  813. ;-----------------------------------------------------------------------------
  814. ; initialisation IDE ATA code
  815. ;-----------------------------------------------------------------------------
  816. include 'detect/init_ata.inc'
  817. ;-----------------------------------------------------------------------------
  818. ; initialisation AHCI code
  819. ;-----------------------------------------------------------------------------
  820.         jmp     ahci_code_end
  821. include 'blkdev/ahci.inc'
  822. ahci_code_end:
  823.         call    ahci_init
  824. ;-----------------------------------------------------------------------------
  825. if 0
  826.         mov     ax, [BOOT.sys_disk]
  827.         cmp     ax, 'r1'; if using not ram disk, then load librares and parameters {SPraid.simba}
  828.         je      no_lib_load
  829.  
  830.         mov     esi, boot_loadlibs
  831.         call    boot_log
  832. ; LOADING LIBRARES
  833.         stdcall dll.Load, @IMPORT           ; loading librares for kernel (.obj files)
  834.         call    load_file_parse_table       ; prepare file parse table
  835.         call    set_kernel_conf             ; configure devices and gui
  836. no_lib_load:
  837. end if
  838.  
  839. ; Display APIC status
  840.         mov     esi, boot_APIC_found
  841.         cmp     [irq_mode], IRQ_APIC
  842.         je      @f
  843.         mov     esi, boot_APIC_nfound
  844. @@:
  845.         call    boot_log
  846.  
  847. ; PRINT AMOUNT OF MEMORY
  848.         mov     esi, boot_memdetect
  849.         call    boot_log
  850.  
  851.         movzx   ecx, word [boot_y]
  852.         if lang eq ru
  853.         or      ecx, (10+30*6) shl 16
  854.         else if lang eq sp
  855.         or      ecx, (10+33*6) shl 16
  856.         else
  857.         or      ecx, (10+29*6) shl 16
  858.         end if
  859.         sub     ecx, 10
  860.         mov     edx, 0xFFFFFF
  861.         mov     ebx, [MEM_AMOUNT]
  862.         shr     ebx, 20
  863.         xor     edi, edi
  864.         mov     eax, 0x00040000
  865.         inc     edi
  866.         call    display_number_force
  867.  
  868. ; BUILD SCHEDULER
  869.  
  870. ;        call    build_scheduler; sys32.inc
  871.  
  872. ;        mov     esi, boot_devices
  873. ;        call    boot_log
  874.  
  875. include "detect/vortex86.inc"                     ; Vortex86 SoC detection code
  876.  
  877.         stdcall load_pe_driver, szVidintel, 0
  878.  
  879.         call    usb_init
  880.  
  881. ; SET PRELIMINARY WINDOW STACK AND POSITIONS
  882.  
  883.         mov     esi, boot_windefs
  884.         call    boot_log
  885.         call    set_window_defaults
  886.  
  887. ; SET BACKGROUND DEFAULTS
  888.  
  889.         mov     esi, boot_bgr
  890.         call    boot_log
  891.         call    init_background
  892.         call    calculatebackground
  893.  
  894. ; RESERVE SYSTEM IRQ'S JA PORT'S
  895.  
  896.         mov     esi, boot_resirqports
  897.         call    boot_log
  898.         call    reserve_irqs_ports
  899.  
  900.         mov     [SLOT_BASE + APPDATA.window], window_data
  901.         mov     [SLOT_BASE + sizeof.APPDATA + APPDATA.window], window_data + sizeof.WDATA
  902.         mov     [SLOT_BASE + sizeof.APPDATA*2 + APPDATA.window], window_data + sizeof.WDATA*2
  903.         mov     [window_data + WDATA.thread], SLOT_BASE
  904.         mov     [window_data + sizeof.WDATA + WDATA.thread], SLOT_BASE + sizeof.APPDATA
  905.         mov     [window_data + sizeof.WDATA*2 + WDATA.thread], SLOT_BASE + sizeof.APPDATA*2
  906.  
  907.         call    init_display
  908.         mov     eax, [def_cursor]
  909.         mov     [window_data + sizeof.WDATA + WDATA.cursor], eax
  910.         mov     [window_data + sizeof.WDATA*2 + WDATA.cursor], eax
  911.  
  912. ; PRINT CPU FREQUENCY
  913.  
  914.         mov     esi, boot_cpufreq
  915.         call    boot_log
  916.  
  917.         cli
  918.         mov     ebx, [hpet_base]
  919.         test    ebx, ebx
  920.         jz      @F
  921.         mov     ebx, [ebx + HPET_COUNTER]
  922.  
  923.         rdtsc
  924.         mov     ecx, 1000
  925.         sub     eax, [hpet_tsc_start]
  926.         sbb     edx, [hpet_tsc_start + 4]
  927.         shld    edx, eax, 10
  928.         shl     eax, 10
  929.         mov     esi, eax
  930.         mov     eax, edx
  931.         mul     ecx
  932.         xchg    eax, esi
  933.         mul     ecx
  934.         adc     edx, esi
  935.         div     ebx
  936.         mul     ecx
  937.         div     [hpet_period]
  938.         mul     ecx
  939.         DEBUGF  1, "K : cpu frequency %u Hz\n", eax
  940.         jmp     .next
  941. @@:
  942.         rdtsc
  943.         mov     ecx, eax
  944.         mov     esi, 250            ; wait 1/4 a second
  945.         call    delay_ms
  946.         rdtsc
  947.  
  948.         sub     eax, ecx
  949.         xor     edx, edx
  950.         shld    edx, eax, 2
  951.         shl     eax, 2
  952. .next:
  953.         mov     dword [cpu_freq], eax
  954.         mov     dword [cpu_freq + 4], edx
  955.         mov     ebx, 1000000
  956.         div     ebx
  957.         mov     ebx, eax
  958.  
  959.         movzx   ecx, word [boot_y]
  960.         if lang eq ru
  961.         add     ecx, (10+19*6) shl 16 - 10
  962.         else if lang eq sp
  963.         add     ecx, (10+25*6) shl 16 - 10
  964.         else
  965.         add     ecx, (10+17*6) shl 16 - 10
  966.         end if
  967.  
  968.         mov     edx, 0xFFFFFF
  969.         xor     edi, edi
  970.         mov     eax, 0x00040000
  971.         inc     edi
  972.         call    display_number_force
  973.  
  974. ; SET VARIABLES
  975.  
  976.         call    set_variables
  977.  
  978. ; STACK AND FDC
  979.  
  980.         call    stack_init
  981.         call    fdc_init
  982.  
  983. ; PALETTE FOR 320x200 and 640x480 16 col
  984.  
  985.         cmp     [SCR_MODE], word 0x12
  986.         jne     no_pal_vga
  987.         mov     esi, boot_pal_vga
  988.         call    boot_log
  989.         call    paletteVGA
  990.       no_pal_vga:
  991.  
  992.         cmp     [SCR_MODE], word 0x13
  993.         jne     no_pal_ega
  994.         mov     esi, boot_pal_ega
  995.         call    boot_log
  996.         call    palette320x200
  997.       no_pal_ega:
  998.  
  999. ; LOAD DEFAULT SKIN
  1000.  
  1001.         call    load_default_skin
  1002.  
  1003. ; Protect I/O permission map
  1004.  
  1005.         mov     esi, [default_io_map]
  1006.         stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
  1007.         add     esi, 0x1000
  1008.         stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
  1009.  
  1010.         stdcall map_page, tss._io_map_0, \
  1011.                 [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
  1012.         stdcall map_page, tss._io_map_1, \
  1013.                 [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
  1014.  
  1015. ; SET KEYBOARD PARAMETERS
  1016.         mov     al, 0xf6       ; reset keyboard, scan enabled
  1017.         call    kb_write_wait_ack
  1018.         test    ah, ah
  1019.         jnz     .no_keyboard
  1020.  
  1021. iglobal
  1022. align 4
  1023. ps2_keyboard_functions:
  1024.         dd      .end - $
  1025.         dd      0       ; no close
  1026.         dd      ps2_set_lights
  1027. .end:
  1028. endg
  1029.         stdcall register_keyboard, ps2_keyboard_functions, 0
  1030.        ; mov   al, 0xED       ; Keyboard LEDs - only for testing!
  1031.        ; call  kb_write_wait_ack
  1032.        ; mov   al, 111b
  1033.        ; call  kb_write_wait_ack
  1034.  
  1035.         mov     al, 0xF3     ; set repeat rate & delay
  1036.         call    kb_write_wait_ack
  1037.         mov     al, 0; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  1038.         call    kb_write_wait_ack
  1039.      ;// mike.dld [
  1040.         call    set_lights
  1041.      ;// mike.dld ]
  1042.         stdcall attach_int_handler, 1, irq1, 0
  1043.         DEBUGF  1, "K : IRQ1 return code %x\n", eax
  1044. .no_keyboard:
  1045.  
  1046. ; Load PS/2 mouse driver
  1047.         mov     esi, boot_setmouse
  1048.         call    boot_log
  1049.         stdcall load_pe_driver, szPS2MDriver, 0
  1050.  
  1051. ; LOAD FIRST APPLICATION
  1052.         cmp     byte [launcher_start], 1        ; Check if starting LAUNCHER is selected on blue screen (1 = yes)
  1053.         jnz     first_app_found
  1054.  
  1055.         cli
  1056.         mov     ebp, firstapp
  1057.         call    fs_execute_from_sysdir
  1058.         test    eax, eax
  1059.         jns     first_app_found
  1060.  
  1061.         mov     esi, boot_failed
  1062.         call    boot_log
  1063.  
  1064.         mov     eax, 0xDEADBEEF      ; otherwise halt
  1065.         hlt
  1066.  
  1067. first_app_found:
  1068.  
  1069. ; START MULTITASKING
  1070. preboot_blogesc = 0       ; start immediately after bootlog
  1071.  
  1072. ; A 'All set - press ESC to start' messages if need
  1073. if preboot_blogesc
  1074.         mov     esi, boot_tasking
  1075.         call    boot_log
  1076. .bll1:
  1077.         in      al, 0x60        ; wait for ESC key press
  1078.         cmp     al, 129
  1079.         jne     .bll1
  1080. end if
  1081.  
  1082.         mov     [timer_ticks_enable], 1         ; for cd driver
  1083.         sti
  1084.  
  1085.         call    mtrr_validate
  1086.  
  1087.         jmp     osloop
  1088.         ; Fly :)
  1089.  
  1090. uglobal
  1091. align 4
  1092. ap_initialized  dd      0
  1093. endg
  1094.  
  1095. ap_init_high:
  1096.         mov     ax, os_stack
  1097.         mov     bx, app_data
  1098.         mov     cx, app_tls
  1099.         mov     ss, ax
  1100.         mov     ds, bx
  1101.         mov     es, bx
  1102.         mov     fs, cx
  1103.         mov     gs, bx
  1104.         xor     esp, esp
  1105.         mov     eax, sys_proc - OS_BASE + PROC.pdt_0
  1106.         mov     cr3, eax
  1107.         lock inc [ap_initialized]
  1108.         jmp     idle_loop
  1109.  
  1110.  
  1111. include 'unpacker.inc'
  1112.  
  1113. align 4
  1114. boot_log:
  1115.         pushad
  1116.  
  1117.         mov     ebx, 10*65536
  1118.         mov     bx, word [boot_y]
  1119.         add     [boot_y], dword 10
  1120.         mov     ecx, 0x80ffffff; ASCIIZ string with white color
  1121.         xor     edi, edi
  1122.         mov     edx, esi
  1123.         inc     edi
  1124.         call    dtext
  1125.  
  1126.         mov     [novesachecksum], 1000
  1127.         call    checkVga_N13
  1128.  
  1129.         popad
  1130.  
  1131.         ret
  1132.  
  1133. ;-----------------------------------------------------------------------------
  1134. ; Register ramdisk file system
  1135. register_ramdisk:
  1136.         mov     esi, boot_initramdisk
  1137.         call    boot_log
  1138.         call    ramdisk_init
  1139.         ret
  1140.  
  1141. ; in: edx -> APPDATA for OS/IDLE slot
  1142. ; in: ebx = stack base
  1143. proc setup_os_slot
  1144.         xor     eax, eax
  1145.         mov     ecx, sizeof.APPDATA/4
  1146.         mov     edi, edx
  1147.         rep stosd
  1148.  
  1149.         mov     eax, tss+0x80
  1150.         call    get_pg_addr
  1151.         inc     eax
  1152.         mov     [edx + APPDATA.io_map], eax
  1153.         mov     eax, tss+0x1080
  1154.         call    get_pg_addr
  1155.         inc     eax
  1156.         mov     [edx + APPDATA.io_map + 4], eax
  1157.  
  1158.         mov     dword [edx + APPDATA.pl0_stack], ebx
  1159.         lea     edi, [ebx + RING0_STACK_SIZE]
  1160.         mov     dword [edx + APPDATA.fpu_state], edi
  1161.         mov     dword [edx + APPDATA.saved_esp0], edi
  1162.         mov     dword [edx + APPDATA.saved_esp], edi
  1163.         mov     dword [edx + APPDATA.terminate_protection], 1 ; make unkillable
  1164.  
  1165.         mov     esi, fpu_data
  1166.         mov     ecx, [xsave_area_size]
  1167.         add     ecx, 3
  1168.         shr     ecx, 2
  1169.         rep movsd
  1170.  
  1171.         lea     eax, [edx + APP_EV_OFFSET]
  1172.         mov     dword [edx + APPDATA.fd_ev], eax
  1173.         mov     dword [edx + APPDATA.bk_ev], eax
  1174.  
  1175.         lea     eax, [edx + APP_OBJ_OFFSET]
  1176.         mov     dword [edx + APPDATA.fd_obj], eax
  1177.         mov     dword [edx + APPDATA.bk_obj], eax
  1178.  
  1179.         mov     dword [edx + APPDATA.cur_dir], sysdir_path-2
  1180.  
  1181.         mov     [edx + APPDATA.process], sys_proc
  1182.  
  1183.         lea     ebx, [edx + APPDATA.list]
  1184.         lea     ecx, [sys_proc + PROC.thr_list]
  1185.         list_add_tail ebx, ecx
  1186.  
  1187.         mov     [edx + APPDATA.wnd_number], dh
  1188.         mov     byte [edx + APPDATA.tid], dh
  1189.         movzx   eax, dh
  1190.         shl     eax, BSF sizeof.WDATA
  1191.         add     eax, window_data
  1192.         mov     [edx + APPDATA.window], eax
  1193.  
  1194.         ret
  1195. endp
  1196.  
  1197. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1198. ;                                                                    ;
  1199. ;                    MAIN OS LOOP START                              ;
  1200. ;                                                                    ;
  1201. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1202. align 32
  1203. osloop:
  1204.         mov     edx, osloop_has_work?
  1205.         xor     ecx, ecx
  1206.         call    Wait_events
  1207.         xor     eax, eax
  1208.         xchg    eax, [osloop_nonperiodic_work]
  1209.         test    eax, eax
  1210.         jz      .no_periodic
  1211.  
  1212.         call    __sys_draw_pointer
  1213.         call    window_check_events
  1214.         call    mouse_check_events
  1215.         call    checkmisc
  1216.         call    checkVga_N13
  1217. ;--------------------------------------
  1218. .no_periodic:
  1219.         call    stack_handler
  1220.         call    check_fdd_motor_status
  1221.         call    check_ATAPI_device_event
  1222.         call    check_lights_state
  1223.         call    check_timers
  1224.  
  1225.         jmp     osloop
  1226. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1227. ;                                                                    ;
  1228. ;                      MAIN OS LOOP END                              ;
  1229. ;                                                                    ;
  1230. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1231. proc osloop_has_work?
  1232.         cmp     [osloop_nonperiodic_work], 0
  1233.         jnz     .yes
  1234.         call    stack_handler_has_work?
  1235.         jnz     .yes
  1236.         call    check_fdd_motor_status_has_work?
  1237.         jnz     .yes
  1238.         call    check_ATAPI_device_event_has_work?
  1239.         jnz     .yes
  1240.         call    check_lights_state_has_work?
  1241.         jnz     .yes
  1242.         call    check_timers_has_work?
  1243.         jnz     .yes
  1244. .no:
  1245.         xor     eax, eax
  1246.         ret
  1247. .yes:
  1248.         xor     eax, eax
  1249.         inc     eax
  1250.         ret
  1251. endp
  1252.  
  1253. proc wakeup_osloop
  1254.         mov     [osloop_nonperiodic_work], 1
  1255.         ret
  1256. endp
  1257.  
  1258. uglobal
  1259. align 4
  1260. osloop_nonperiodic_work dd      ?
  1261. endg
  1262.  
  1263. uglobal
  1264. align 64
  1265. idle_addr       rb      64
  1266. endg
  1267.  
  1268. idle_thread:
  1269.         sti
  1270.  
  1271. ; The following code can be executed by all CPUs in the system.
  1272. ; All other parts of the kernel do not expect multi-CPU.
  1273. ; Also, APs don't even have a stack here.
  1274. ; Beware. Don't do anything here. Anything at all.
  1275. idle_loop:
  1276.         cmp     [use_mwait_for_idle], 0
  1277.         jnz     .mwait
  1278.  
  1279. .hlt:
  1280.         hlt
  1281.         jmp     .hlt
  1282.  
  1283. .mwait:
  1284.         mov     eax, idle_addr
  1285.         xor     ecx, ecx
  1286.         xor     edx, edx
  1287.         monitor
  1288.         xor     ecx, ecx
  1289.         mov     eax, 20h        ; or 10h
  1290.         mwait
  1291.         jmp     .mwait
  1292.  
  1293.  
  1294. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1295. ;                                                                      ;
  1296. ;                   INCLUDED SYSTEM FILES                              ;
  1297. ;                                                                      ;
  1298. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1299.  
  1300.  
  1301. include "kernel32.inc"
  1302.  
  1303.  
  1304. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1305. ;                                                                      ;
  1306. ;                       KERNEL FUNCTIONS                               ;
  1307. ;                                                                      ;
  1308. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1309.  
  1310. reserve_irqs_ports:
  1311.  
  1312.  
  1313. ; RESERVE PORTS
  1314.         mov     eax, RESERVED_PORTS
  1315.         mov     ecx, 1
  1316.  
  1317.         mov     [eax], dword 4
  1318.  
  1319.         mov     [eax+16], ecx
  1320.         mov     [eax+16+4], dword 0
  1321.         mov     [eax+16+8], dword 0x2D
  1322.  
  1323.         mov     [eax+32], ecx
  1324.         mov     [eax+32+4], dword 0x30
  1325.         mov     [eax+32+8], dword 0x4D
  1326.  
  1327.         mov     [eax+48], ecx
  1328.         mov     [eax+48+4], dword 0x50
  1329.         mov     [eax+48+8], dword 0xDF
  1330.  
  1331.         mov     [eax+64], ecx
  1332.         mov     [eax+64+4], dword 0xE5
  1333.         mov     [eax+64+8], dword 0xFF
  1334.  
  1335.         ret
  1336.  
  1337.  
  1338. iglobal
  1339.   process_number dd 0x2
  1340. endg
  1341.  
  1342. set_variables:
  1343.  
  1344.         mov     ecx, 0x16                    ; flush port 0x60
  1345. @@:
  1346.         in      al, 0x60
  1347.         loop    @b
  1348.         push    eax
  1349.  
  1350.         mov     ax, [BOOT.y_res]
  1351.         shr     ax, 1
  1352.         shl     eax, 16
  1353.         mov     ax, [BOOT.x_res]
  1354.         shr     ax, 1
  1355.         mov     [MOUSE_X], eax
  1356.         call    wakeup_osloop
  1357.  
  1358.         xor     eax, eax
  1359.         mov     [BTN_ADDR], dword BUTTON_INFO ; address of button list
  1360.  
  1361.         mov     byte [KEY_COUNT], al              ; keyboard buffer
  1362.         mov     byte [BTN_COUNT], al              ; button buffer
  1363.  
  1364.         pop     eax
  1365.         ret
  1366. ;-----------------------------------------------------------------------------
  1367.  
  1368. align 4
  1369. display_number:
  1370. ; add check pointers
  1371.         test    bl, bl
  1372.         jz      @f
  1373.         bt      ebx, 30  ; check 30 bit
  1374.         jb      @f
  1375.         stdcall is_region_userspace, ecx, 4
  1376.         jz      @f
  1377.         ret
  1378. @@:
  1379.         jz      @f
  1380.         stdcall is_region_userspace, ecx, 8
  1381.         jz      @f
  1382.         ret
  1383. @@:
  1384.         test    esi, 0x08000000
  1385.         jz      @f
  1386.         stdcall is_region_userspace, edi, 1
  1387.         jz      @f
  1388.         ret
  1389. @@:
  1390. ;It is not optimization
  1391.         mov     eax, ebx
  1392.         mov     ebx, ecx
  1393.         mov     ecx, edx
  1394.         mov     edx, esi
  1395.         mov     esi, edi
  1396. ; eax = print type, al=0 -> ebx is number
  1397. ;                   al=1 -> ebx is pointer
  1398. ;                   ah=0 -> display decimal
  1399. ;                   ah=1 -> display hexadecimal
  1400. ;                   ah=2 -> display binary
  1401. ;                   eax bits 16-21 = number of digits to display (0-32)
  1402. ;                   eax bits 22-31 = reserved
  1403. ;
  1404. ; ebx = number or pointer
  1405. ; ecx = x shl 16 + y
  1406. ; edx = color
  1407.         xor     edi, edi
  1408. display_number_force:
  1409.         push    eax
  1410.         and     eax, 0x3fffffff
  1411.         cmp     eax, 0xffff     ; length > 0 ?
  1412.         pop     eax
  1413.         jge     .cont_displ
  1414.         ret
  1415.    .cont_displ:
  1416.         push    eax
  1417.         and     eax, 0x3fffffff
  1418.         cmp     eax, 61*0x10000  ; length <= 60 ?
  1419.         pop     eax
  1420.         jb      .cont_displ2
  1421.         ret
  1422.    .cont_displ2:
  1423.  
  1424.         pushad
  1425.  
  1426.         cmp     al, 1            ; ecx is a pointer ?
  1427.         jne     @f
  1428.         mov     ebp, ebx
  1429.         add     ebp, 4
  1430.         mov     ebp, [ebp] ;[ebp + std_application_base_address]
  1431.         mov     ebx, [ebx] ;[ebx + std_application_base_address]
  1432. @@:
  1433.         sub     esp, 64
  1434.  
  1435.         test    ah, ah            ; DECIMAL
  1436.         jnz     .no_display_desnum
  1437.  
  1438.         shr     eax, 16
  1439.         and     eax, 0xC03f
  1440. ;     and   eax,0x3f
  1441.         push    eax
  1442.         and     eax, 0x3f
  1443.         mov     edi, esp
  1444.         add     edi, 4+64-1
  1445.         mov     ecx, eax
  1446.         mov     eax, ebx
  1447.         mov     ebx, 10
  1448. @@:
  1449.         xor     edx, edx
  1450.         call    division_64_bits
  1451.         div     ebx
  1452.         add     dl, 48
  1453.         mov     [edi], dl
  1454.         dec     edi
  1455.         loop    @b
  1456.  
  1457.         pop     eax
  1458.         call    normalize_number
  1459.         call    draw_num_text
  1460.         add     esp, 64
  1461.         popad
  1462.         ret
  1463. .no_display_desnum:
  1464.  
  1465.         cmp     ah, 0x01         ; HEXADECIMAL
  1466.         jne     .no_display_hexnum
  1467.  
  1468.         shr     eax, 16
  1469.         and     eax, 0xC03f
  1470. ;     and   eax,0x3f
  1471.         push    eax
  1472.         and     eax, 0x3f
  1473.         mov     edi, esp
  1474.         add     edi, 4+64-1
  1475.         mov     ecx, eax
  1476.         mov     eax, ebx
  1477.         mov     ebx, 16
  1478. @@:
  1479.         xor     edx, edx
  1480.         call    division_64_bits
  1481.         div     ebx
  1482.    ;hexletters = __fdo_hexdigits
  1483.         add     edx, __fdo_hexdigits ;hexletters
  1484.         mov     dl, [edx]
  1485.         mov     [edi], dl
  1486.         dec     edi
  1487.         loop    @b
  1488.  
  1489.         pop     eax
  1490.         call    normalize_number
  1491.         call    draw_num_text
  1492.         add     esp, 64
  1493.         popad
  1494.         ret
  1495. .no_display_hexnum:
  1496.  
  1497.         cmp     ah, 0x02         ; BINARY
  1498.         jne     .no_display_binnum
  1499.  
  1500.         shr     eax, 16
  1501.         and     eax, 0xC03f
  1502. ;     and   eax,0x3f
  1503.         push    eax
  1504.         and     eax, 0x3f
  1505.         mov     edi, esp
  1506.         add     edi, 4+64-1
  1507.         mov     ecx, eax
  1508.         mov     eax, ebx
  1509.         mov     ebx, 2
  1510. @@:
  1511.         xor     edx, edx
  1512.         call    division_64_bits
  1513.         div     ebx
  1514.         add     dl, 48
  1515.         mov     [edi], dl
  1516.         dec     edi
  1517.         loop    @b
  1518.  
  1519.         pop     eax
  1520.         call    normalize_number
  1521.         call    draw_num_text
  1522.         add     esp, 64
  1523.         popad
  1524.         ret
  1525. .no_display_binnum:
  1526.  
  1527.         add     esp, 64
  1528.         popad
  1529.         ret
  1530.  
  1531. normalize_number:
  1532.         test    ah, 0x80
  1533.         jz      .continue
  1534.         mov     ecx, 48
  1535.         and     eax, 0x3f
  1536. @@:
  1537.         inc     edi
  1538.         cmp     [edi], cl
  1539.         jne     .continue
  1540.         dec     eax
  1541.         cmp     eax, 1
  1542.         ja      @r
  1543.         mov     al, 1
  1544. .continue:
  1545.         and     eax, 0x3f
  1546.         ret
  1547.  
  1548. division_64_bits:
  1549.         test    [esp+1+4], byte 0x40
  1550.         jz      .continue
  1551.         push    eax
  1552.         mov     eax, ebp
  1553.         div     ebx
  1554.         mov     ebp, eax
  1555.         pop     eax
  1556. .continue:
  1557.         ret
  1558.  
  1559. draw_num_text:
  1560.         mov     esi, eax
  1561.         mov     edx, 64+4
  1562.         sub     edx, eax
  1563.         add     edx, esp
  1564.         mov     ebx, [esp+64+32-8+4]
  1565. ; add window start x & y
  1566.  
  1567.         mov     ecx, [current_slot]
  1568.         mov     ecx, [ecx + APPDATA.window]
  1569.  
  1570.         mov     eax, [ecx + WDATA.box.left]
  1571.         add     eax, [ecx + WDATA.clientbox.left]
  1572.         shl     eax, 16
  1573.         add     eax, [ecx + WDATA.box.top]
  1574.         add     eax, [ecx + WDATA.clientbox.top]
  1575.         add     ebx, eax
  1576.         mov     ecx, [esp+64+32-12+4]
  1577.         mov     eax, [esp+64+8]         ; background color (if given)
  1578.         mov     edi, [esp+64+4]
  1579.         and     ecx, 5FFFFFFFh
  1580.         bt      ecx, 27
  1581.         jnc     @f
  1582.         mov     edi, eax
  1583. @@:
  1584.         jmp     dtext
  1585. ;-----------------------------------------------------------------------------
  1586. align 4
  1587. sys_setup:
  1588. ;  1 = roland mpu midi base , base io address
  1589. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1590. ;  3 = not used
  1591. ;  4 = not used
  1592. ;  5 = system language, 1eng 2fi 3ger 4rus
  1593. ;  6 = not used
  1594. ;  7 = not used
  1595. ;  8 = not used
  1596. ;  9 = not used
  1597. ; 10 = not used
  1598. ; 11 = enable lba read
  1599. ; 12 = enable pci access
  1600. ;-----------------------------------------------------------------------------
  1601.         and     [esp + SYSCALL_STACK.eax], dword 0
  1602. ; F.21.1 - set MPU MIDI base port
  1603.         dec     ebx
  1604.         jnz     @f
  1605.  
  1606.         cmp     ecx, 0x100
  1607.         jb      @f
  1608.  
  1609.         mov     esi, 65535
  1610.         cmp     esi, ecx
  1611.         jb      @f
  1612.  
  1613.         mov     [midi_base], cx
  1614.         mov     word [mididp], cx
  1615.         inc     cx
  1616.         mov     word [midisp], cx
  1617.         ret
  1618. ;--------------------------------------
  1619. @@:
  1620. ; F.21.2 - set keyboard layout
  1621.         dec     ebx
  1622.         jnz     @f
  1623.  
  1624.         mov     eax, edx
  1625. ; 1 = normal layout
  1626.         dec     ecx
  1627.         jnz     .shift
  1628.  
  1629.         mov     ebx, keymap
  1630.         mov     ecx, 128
  1631.         call    memmove
  1632.         ret
  1633. ;--------------------------------------
  1634. .shift:
  1635. ; 2 = layout at pressed Shift
  1636.         dec     ecx
  1637.         jnz     .alt
  1638.  
  1639.         mov     ebx, keymap_shift
  1640.         mov     ecx, 128
  1641.         call    memmove
  1642.         ret
  1643. ;--------------------------------------
  1644. .alt:
  1645. ; 3 = layout at pressed Alt
  1646.         dec     ecx
  1647.         jnz     .country
  1648.  
  1649.         mov     ebx, keymap_alt
  1650.         mov     ecx, 128
  1651.         call    memmove
  1652.         ret
  1653. ;--------------------------------------
  1654. .country:
  1655. ; country identifier
  1656.         sub     ecx, 6
  1657.         jnz     .error
  1658.  
  1659.         mov     word [keyboard], dx
  1660.         ret
  1661. ;--------------------------------------
  1662. @@:
  1663. ; F.21.5 - set system language
  1664.         sub     ebx, 3
  1665.         jnz     @f
  1666.  
  1667.         mov     [syslang], ecx
  1668.         ret
  1669. ;--------------------------------------
  1670. @@:
  1671. ; F.21.11 - enable/disable low-level access to HD
  1672.         and     ecx, 1
  1673.         sub     ebx, 6
  1674.         jnz     @f
  1675.  
  1676.         mov     [lba_read_enabled], ecx
  1677.         ret
  1678. ;--------------------------------------
  1679. @@:
  1680. ; F.21.12 - enable/disable low-level access to PCI
  1681.         dec     ebx
  1682.         jnz     .error
  1683.  
  1684.         mov     [pci_access_enabled], ecx
  1685.         ret
  1686. ;--------------------------------------
  1687. .error:
  1688.         or      [esp + SYSCALL_STACK.eax], dword -1
  1689.         ret
  1690. ;-----------------------------------------------------------------------------
  1691. align 4
  1692. sys_getsetup:
  1693. ;  1 = roland mpu midi base , base io address
  1694. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1695. ;  3 = not used
  1696. ;  4 = not used
  1697. ;  5 = system language, 1eng 2fi 3ger 4rus
  1698. ;  6 = not used
  1699. ;  7 = not used
  1700. ;  8 = not used
  1701. ;  9 = get hs timer tic
  1702. ; 10 = not used
  1703. ; 11 = get the state "lba read"
  1704. ; 12 = get the state "pci access"
  1705. ;-----------------------------------------------------------------------------
  1706. ; F.26.1 - get MPU MIDI base port
  1707.         dec     ebx
  1708.         jnz     @f
  1709.  
  1710.         movzx   eax, [midi_base]
  1711.         mov     [esp + SYSCALL_STACK.eax], eax
  1712.         ret
  1713. ;--------------------------------------
  1714. @@:
  1715. ; F.26.2 - get keyboard layout
  1716.         dec     ebx
  1717.         jnz     @f
  1718.  
  1719.         mov     ebx, edx
  1720.         ; if given memory address belongs to kernel then error
  1721.         stdcall is_region_userspace, ebx, 128
  1722.         jnz     .addr_error
  1723. ; 1 = normal layout
  1724.         dec     ecx
  1725.         jnz     .shift
  1726.  
  1727.         mov     eax, keymap
  1728.         mov     ecx, 128
  1729.         call    memmove
  1730.         ret
  1731. ;--------------------------------------
  1732. .shift:
  1733. ; 2 = layout with pressed Shift
  1734.         dec     ecx
  1735.         jnz     .alt
  1736.  
  1737.         mov     eax, keymap_shift
  1738.         mov     ecx, 128
  1739.         call    memmove
  1740.         ret
  1741. ;--------------------------------------
  1742. .alt:
  1743. ; 3 = layout with pressed Alt
  1744.         dec     ecx
  1745.         jne     .country
  1746.  
  1747.         mov     eax, keymap_alt
  1748.         mov     ecx, 128
  1749.         call    memmove
  1750.         ret
  1751. ;--------------------------------------
  1752. .country:
  1753. ; 9 = country identifier
  1754.         sub     ecx, 6
  1755.         jnz     .error
  1756.  
  1757.         movzx   eax, word [keyboard]
  1758.         mov     [esp + SYSCALL_STACK.eax], eax
  1759.         ret
  1760.  
  1761. .addr_error:    ; if given memory address is illegal
  1762.         or      dword [esp + SYSCALL_STACK.eax], -1
  1763.         ret        
  1764. ;--------------------------------------
  1765. @@:
  1766. ; F.26.5 - get system language
  1767.         sub     ebx, 3
  1768.         jnz     @f
  1769.  
  1770.         mov     eax, [syslang]
  1771.         mov     [esp + SYSCALL_STACK.eax], eax
  1772.         ret
  1773. ;--------------------------------------
  1774. @@:
  1775. ; F.26.9 - get the value of the time counter
  1776.         sub     ebx, 4
  1777.         jnz     @f
  1778.  
  1779.         mov     eax, [timer_ticks]
  1780.         mov     [esp + SYSCALL_STACK.eax], eax
  1781.         ret
  1782. ;--------------------------------------
  1783. @@:
  1784. ; F.26.10 - get the time from kernel launch in nanoseconds
  1785.         dec     ebx
  1786.         jnz     @f
  1787.  
  1788.         call    get_clock_ns
  1789.         mov     [esp + SYSCALL_STACK.edx], edx
  1790.         mov     [esp + SYSCALL_STACK.eax], eax
  1791.         ret
  1792. ;--------------------------------------
  1793. @@:
  1794. ; F.26.11 - Find out whether low-level HD access is enabled
  1795.         dec     ebx
  1796.         jnz     @f
  1797.  
  1798.         mov     eax, [lba_read_enabled]
  1799.         mov     [esp + SYSCALL_STACK.eax], eax
  1800.         ret
  1801. ;--------------------------------------
  1802. @@:
  1803. ; F.26.12 - Find out whether low-level PCI access is enabled
  1804.         dec     ebx
  1805.         jnz     .error
  1806.  
  1807.         mov     eax, [pci_access_enabled]
  1808.         mov     [esp + SYSCALL_STACK.eax], eax
  1809.         ret
  1810. ;--------------------------------------
  1811. .error:
  1812.         or      [esp + SYSCALL_STACK.eax], dword -1
  1813.         ret
  1814. ;-----------------------------------------------------------------------------
  1815. get_timer_ticks:
  1816.         mov     eax, [timer_ticks]
  1817.         ret
  1818. ;-----------------------------------------------------------------------------
  1819. iglobal
  1820. midi_base dw 0
  1821. endg
  1822.  
  1823. align 4
  1824. sys_midi:
  1825.         cmp     word [mididp], 0
  1826.         jnz     @f
  1827.         mov     [esp + SYSCALL_STACK.eax], 1
  1828.         ret
  1829. @@:
  1830.         and     [esp + SYSCALL_STACK.eax], 0
  1831.         dec     ebx
  1832.         jnz     .smn1
  1833.  ;    call setuart
  1834. @@:
  1835.         call    .is_output
  1836.         test    al, al
  1837.         jnz     @b
  1838.         mov     dx, word [midisp]
  1839.         mov     al, 0xff
  1840.         out     dx, al
  1841. @@:
  1842.         mov     dx, word [midisp]
  1843.         mov     al, 0xff
  1844.         out     dx, al
  1845.         call    .is_input
  1846.         test    al, al
  1847.         jnz     @b
  1848.         call    .get_mpu_in
  1849.         cmp     al, 0xfe
  1850.         jnz     @b
  1851. @@:
  1852.         call    .is_output
  1853.         test    al, al
  1854.         jnz     @b
  1855.         mov     dx, word [midisp]
  1856.         mov     al, 0x3f
  1857.         out     dx, al
  1858.         ret
  1859. .smn1:
  1860.         dec     ebx
  1861.         jnz     .ret
  1862. @@:
  1863.         call    .get_mpu_in
  1864.         call    .is_output
  1865.         test    al, al
  1866.         jnz     @b
  1867.         mov     al, bl
  1868.         call    .put_mpu_out
  1869. .ret:
  1870.         ret
  1871.  
  1872. .is_input:
  1873.         push    edx
  1874.         mov     dx, word [midisp]
  1875.         in      al, dx
  1876.         and     al, 0x80
  1877.         pop     edx
  1878.         ret
  1879.  
  1880. .is_output:
  1881.         push    edx
  1882.         mov     dx, word [midisp]
  1883.         in      al, dx
  1884.         and     al, 0x40
  1885.         pop     edx
  1886.         ret
  1887.  
  1888. .get_mpu_in:
  1889.         push    edx
  1890.         mov     dx, word [mididp]
  1891.         in      al, dx
  1892.         pop     edx
  1893.         ret
  1894.  
  1895. .put_mpu_out:
  1896.         push    edx
  1897.         mov     dx, word [mididp]
  1898.         out     dx, al
  1899.         pop     edx
  1900.         ret
  1901.  
  1902. ;-----------------------------------------------------------------------------
  1903. sys_end:
  1904.         cmp     [_display.select_cursor], 0
  1905.         je      @f
  1906. ; restore default cursor before killing
  1907.         pusha
  1908.         mov     ecx, [current_slot]
  1909.         mov     ecx, [ecx + APPDATA.window]
  1910.         call    restore_default_cursor_before_killing
  1911.         popa
  1912. @@:
  1913. ;--------------------------------------
  1914. ; kill all sockets this process owns
  1915.         pusha
  1916.         mov     edx, [current_slot]
  1917.         mov     edx, [edx + APPDATA.tid]
  1918.         call    socket_process_end
  1919.         popa
  1920. ;--------------------------------------
  1921.         mov     ecx, [current_slot]
  1922.         mov     eax, [ecx + APPDATA.tls_base]
  1923.         test    eax, eax
  1924.         jz      @F
  1925.  
  1926.         stdcall user_free, eax
  1927. @@:
  1928.  
  1929.         mov     eax, [current_slot]
  1930.         mov     [eax + APPDATA.state], TSTATE_ZOMBIE
  1931.         call    wakeup_osloop
  1932.  
  1933. .waitterm:            ; wait here for termination
  1934.         call    change_task
  1935.         jmp     .waitterm
  1936. ;------------------------------------------------------------------------------
  1937. align 4
  1938. ; ecx - ptr WDATA
  1939. restore_default_cursor_before_killing:
  1940.         pushfd
  1941.         cli
  1942.         mov     eax, [def_cursor]
  1943.         mov     [ecx + WDATA.cursor], eax
  1944.  
  1945.         movzx   eax, word [MOUSE_Y]
  1946.         movzx   ebx, word [MOUSE_X]
  1947.         mov     eax, [d_width_calc_area + eax*4]
  1948.  
  1949.         add     eax, [_display.win_map]
  1950.         movzx   edx, byte [ebx + eax]
  1951.         shl     edx, BSF sizeof.WDATA
  1952.         mov     esi, [window_data + edx + WDATA.cursor]
  1953.  
  1954.         cmp     esi, [current_cursor]
  1955.         je      @f
  1956.  
  1957.         push    esi
  1958.         call    [_display.select_cursor]
  1959.         mov     [current_cursor], esi
  1960. @@:
  1961.         mov     [redrawmouse_unconditional], 1
  1962.         call    wakeup_osloop
  1963.         popfd
  1964.         ret
  1965. ;------------------------------------------------------------------------------
  1966. iglobal
  1967. align 4
  1968. sys_system_table:
  1969.         dd      sysfn_deactivate        ; 1 = deactivate window
  1970.         dd      sysfn_terminate         ; 2 = terminate thread
  1971.         dd      sysfn_activate          ; 3 = activate window
  1972.         dd      sysfn_getidletime       ; 4 = get idle time
  1973.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1974.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1975.         dd      sysfn_getactive         ; 7 = get active window
  1976.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1977.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1978.         dd      sysfn_minimize          ; 10 = minimize window
  1979.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1980.         dd      undefined_syscall       ; 12 = get last pressed key. function removed. sysfn_lastkey
  1981.         dd      sysfn_getversion        ; 13 = get kernel version
  1982.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1983.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1984.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1985.         dd      sysfn_getallmem         ; 17 = get total memory size
  1986.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1987.                                         ;                 instead of slot
  1988.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1989.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1990.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  1991.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  1992.         dd      sysfn_min_windows       ; 23 = minimize all windows
  1993.         dd      sysfn_set_screen_sizes  ; 24 = set screen sizes for Vesa
  1994.  
  1995.         dd      sysfn_zmodif            ; 25 = get/set window z modifier  ;Fantomer
  1996. sysfn_num = ($ - sys_system_table)/4
  1997. endg
  1998. ;------------------------------------------------------------------------------
  1999. sys_system:
  2000.         dec     ebx
  2001.         cmp     ebx, sysfn_num
  2002.         jae     @f
  2003.         jmp     dword [sys_system_table + ebx*4]
  2004. @@:
  2005.         ret
  2006. ;------------------------------------------------------------------------------
  2007. sysfn_shutdown:          ; 18.9 = system shutdown
  2008.         cmp     ecx, SYSTEM_SHUTDOWN
  2009.         jl      .exit_for_anyone
  2010.         cmp     ecx, SYSTEM_RESTART
  2011.         jg      .exit_for_anyone
  2012.         mov     [BOOT.shutdown_type], cl
  2013.  
  2014.         mov     eax, [thread_count]
  2015.         mov     [SYS_SHUTDOWN], al
  2016.         mov     [shutdown_processes], eax
  2017.         call    wakeup_osloop
  2018.         and     dword [esp + SYSCALL_STACK.eax], 0
  2019. .exit_for_anyone:
  2020.         ret
  2021.   uglobal
  2022.    shutdown_processes:
  2023.                        dd 0x0
  2024.   endg
  2025. ;------------------------------------------------------------------------------
  2026. ; in: eax -- APPDATA ptr
  2027. ; out: Z/z -- is/not kernel thread
  2028. is_kernel_thread:
  2029.         mov     eax, [eax + APPDATA.process]
  2030.         cmp     eax, [SLOT_BASE + 2*sizeof.APPDATA + APPDATA.process]       ; OS
  2031.         ret
  2032. ;------------------------------------------------------------------------------
  2033. sysfn_terminate:        ; 18.2 = TERMINATE
  2034.         push    ecx
  2035.         cmp     ecx, 2
  2036.         jb      .noprocessterminate
  2037.         mov     edx, [thread_count]
  2038.         cmp     ecx, edx
  2039.         ja      .noprocessterminate
  2040.         mov     eax, [thread_count]
  2041.         shl     ecx, BSF sizeof.APPDATA
  2042.         add     ecx, SLOT_BASE
  2043.  
  2044.         mov     edx, [ecx + APPDATA.tid]
  2045.         cmp     byte [ecx + APPDATA.state], TSTATE_FREE
  2046.         jz      .noprocessterminate
  2047.         push    eax
  2048.         mov     eax, ecx
  2049.         call    is_kernel_thread
  2050.         pop     eax
  2051.         jz      .noprocessterminate
  2052.         push    ecx edx
  2053.         mov     edx, ecx
  2054.         call    request_terminate
  2055.         pop     edx ecx
  2056.         test    eax, eax
  2057.         jz      .noprocessterminate
  2058. ;--------------------------------------
  2059. ; terminate all network sockets it used
  2060.         pusha
  2061.         mov     eax, edx     ;TODO: check function
  2062.         call    socket_process_end
  2063.         popa
  2064. ;--------------------------------------
  2065.         cmp     [_display.select_cursor], 0
  2066.         je      .restore_end
  2067. ; restore default cursor before killing
  2068.         pusha
  2069.         mov     ecx, [esp+32]
  2070.         shl     ecx, BSF sizeof.WDATA
  2071.         add     ecx, window_data
  2072.         mov     eax, [def_cursor]
  2073.         cmp     [ecx + WDATA.cursor], eax
  2074.         je      @f
  2075.         call    restore_default_cursor_before_killing
  2076. @@:
  2077.         popa
  2078. .restore_end:
  2079. ;--------------------------------------
  2080.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2081.         mov     [ecx + APPDATA.state], TSTATE_ZOMBIE; clear possible i40's
  2082.         call    wakeup_osloop
  2083.      ;call MEM_Heap_UnLock
  2084.  
  2085.         cmp     edx, [application_table_owner]; clear app table stat
  2086.         jne     .noatsc
  2087.         call    unlock_application_table
  2088. .noatsc:
  2089. .noprocessterminate:
  2090.         add     esp, 4
  2091.         ret
  2092. ;------------------------------------------------------------------------------
  2093. sysfn_terminate2:
  2094.         call    lock_application_table
  2095.         mov     eax, ecx
  2096.         call    pid_to_slot
  2097.         test    eax, eax
  2098.         jz      .not_found
  2099.         mov     ecx, eax
  2100.         cli
  2101.         call    sysfn_terminate
  2102.         call    unlock_application_table
  2103.         sti
  2104.         and     dword [esp + SYSCALL_STACK.eax], 0
  2105.         ret
  2106. .not_found:
  2107.         call    unlock_application_table
  2108.         or      dword [esp + SYSCALL_STACK.eax], -1
  2109.         ret
  2110. ;------------------------------------------------------------------------------
  2111. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  2112.         cmp     ecx, 2
  2113.         jb      .nowindowdeactivate
  2114.         cmp     ecx, [thread_count]
  2115.         ja      .nowindowdeactivate
  2116.  
  2117.         movzx   esi, word [WIN_STACK + ecx*2]
  2118.         cmp     esi, 1
  2119.         je      .nowindowdeactivate ; already deactive
  2120.  
  2121.         mov     edi, ecx
  2122.         shl     edi, BSF sizeof.WDATA
  2123.         add     edi, window_data
  2124.         movzx   esi, word [WIN_STACK + ecx * 2]
  2125.         lea     esi, [WIN_POS + esi * 2]
  2126.         call    window._.window_deactivate
  2127.         call    syscall_display_settings.calculateScreen
  2128.         call    syscall_display_settings.redrawScreen
  2129. .nowindowdeactivate:
  2130.         ret
  2131. ;------------------------------------------------------------------------------
  2132. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2133.         cmp     ecx, 2
  2134.         jb      .nowindowactivate
  2135.         cmp     ecx, [thread_count]
  2136.         ja      .nowindowactivate
  2137. ;-------------------------------------
  2138. @@:
  2139. ; If the window is captured and moved by the user,
  2140. ; then you can't change the position in window stack!!!
  2141.         mov     al, [mouse.active_sys_window.action]
  2142.         and     al, WINDOW_MOVE_AND_RESIZE_FLAGS
  2143.         test    al, al
  2144.         jz      @f
  2145.         call    change_task
  2146.         jmp     @b
  2147. @@:
  2148. ;-------------------------------------
  2149.         mov     [window_minimize], 2; restore window if minimized
  2150.         call    wakeup_osloop
  2151.  
  2152.         movzx   esi, word [WIN_STACK + ecx*2]
  2153.         cmp     esi, [thread_count]
  2154.         je      .nowindowactivate; already active
  2155.  
  2156.         mov     edi, ecx
  2157.         shl     edi, BSF sizeof.WDATA
  2158.         add     edi, window_data
  2159.         movzx   esi, word [WIN_STACK + ecx * 2]
  2160.         lea     esi, [WIN_POS + esi * 2]
  2161.         call    waredraw
  2162. .nowindowactivate:
  2163.         ret
  2164. ;------------------------------------------------------------------------------
  2165. align 4
  2166. sysfn_zmodif:
  2167. ;18,25,1 - get z_modif
  2168. ;18,25,2 - set z_modif
  2169. ;edx = -1(for current task) or TID
  2170. ;esi(for 2) = new value z_modif
  2171. ;return:
  2172. ;1:   eax = z_modif
  2173. ;2: eax=0(fail),1(success) for set z_modif
  2174.  
  2175.         cmp     edx, -1
  2176.         jne     @f
  2177.         mov     edx, [current_slot_idx]
  2178.      @@:
  2179.         cmp     edx, [thread_count]
  2180.         ja      .fail
  2181.         cmp     edx, 1
  2182.         je      .fail
  2183.  
  2184.         mov     eax, edx
  2185.         shl     edx, BSF sizeof.WDATA
  2186.  
  2187.         test    [window_data + edx + WDATA.fl_wstate], WSTATE_USED
  2188.         jz      .fail
  2189.  
  2190.         cmp     ecx, 1
  2191.         jnz     .set_zmod
  2192.  
  2193.         mov     al, [window_data + edx + WDATA.z_modif]
  2194.         jmp     .exit
  2195.  
  2196. .set_zmod:
  2197.         cmp     ecx, 2
  2198.         jnz     .fail
  2199.  
  2200.         mov     ebx, esi
  2201.         mov     esi, eax
  2202.  
  2203.         cmp     bl, ZPOS_ALWAYS_TOP
  2204.         jg      .fail
  2205.  
  2206.         mov     [window_data + edx + WDATA.z_modif], bl
  2207.  
  2208.         mov     eax, [window_data + edx + WDATA.box.left]
  2209.         mov     ebx, [window_data + edx + WDATA.box.top]
  2210.         mov     ecx, [window_data + edx + WDATA.box.width]
  2211.         mov     edx, [window_data + edx + WDATA.box.height]
  2212.         add     ecx, eax
  2213.         add     edx, ebx
  2214.         call    window._.set_screen
  2215.         call    window._.set_top_wnd
  2216.         call    window._.redraw_top_wnd
  2217.  
  2218.         shl     esi, BSF sizeof.WDATA
  2219.         mov     [esi + window_data + WDATA.fl_redraw], WSTATE_REDRAW
  2220.  
  2221.  
  2222.         mov     eax, 1
  2223.         jmp     .exit
  2224. .fail:
  2225.         xor     eax, eax
  2226. .exit:
  2227.         mov     [esp + SYSCALL_STACK.eax], eax
  2228.         ret
  2229.  
  2230. ;------------------------------------------------------------------------------
  2231. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2232.         ;mov     eax, [TASK_TABLE+32+TASKDATA.cpu_usage]
  2233.         mov     eax, [SLOT_BASE + APPDATA.cpu_usage]
  2234.         mov     [esp + SYSCALL_STACK.eax], eax
  2235.         ret
  2236. ;------------------------------------------------------------------------------
  2237. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2238.         mov     eax, dword [cpu_freq]
  2239.         mov     [esp + SYSCALL_STACK.eax], eax
  2240.         ret
  2241. ;------------------------------------------------------------------------------
  2242. get_cpu_freq:
  2243.         mov     eax, dword [cpu_freq]
  2244.         mov     edx, dword [cpu_freq+4]
  2245.         ret
  2246. ;  SAVE ramdisk to /hd/1/kolibri.img
  2247. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2248.    include 'blkdev/rdsave.inc'
  2249. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2250. ;------------------------------------------------------------------------------
  2251. align 4
  2252. sysfn_getactive:        ; 18.7 = get active window
  2253.         mov     eax, [thread_count]
  2254.         movzx   eax, word [WIN_POS + eax*2]
  2255.         mov     [esp + SYSCALL_STACK.eax], eax
  2256.         ret
  2257. ;------------------------------------------------------------------------------
  2258. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2259. ;     cmp  ecx,1
  2260.         dec     ecx
  2261.         jnz     .set_flag
  2262.         movzx   eax, byte [sound_flag]; get sound_flag
  2263.         mov     [esp + SYSCALL_STACK.eax], eax
  2264.         ret
  2265. .set_flag:
  2266. ;     cmp  ecx,2
  2267.         dec     ecx
  2268.         jnz     .err
  2269.         xor     byte [sound_flag], 1
  2270. .err:
  2271.         ret
  2272. ;------------------------------------------------------------------------------
  2273. sysfn_minimize:         ; 18.10 = minimize window
  2274.         mov     [window_minimize], 1
  2275.         call    wakeup_osloop
  2276.         ret
  2277. ;------------------------------------------------------------------------------
  2278. align 4
  2279. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2280.         dec     ecx
  2281.         jnz     .exit
  2282. .small_table:
  2283.         stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
  2284.         jnz     .exit
  2285.         mov     edi, edx
  2286.         mov     esi, DRIVE_DATA
  2287.         mov     ecx, DRIVE_DATA_SIZE ;10
  2288.         cld
  2289.         rep movsb
  2290. .exit:
  2291.         ret
  2292. ;------------------------------------------------------------------------------
  2293. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2294.         ; if given memory address belongs to kernel then error
  2295.         stdcall is_region_userspace, ecx, version_inf.size
  2296.         jnz     .addr_error
  2297.  
  2298.         mov     edi, ecx
  2299.         mov     esi, version_inf
  2300.         mov     ecx, version_inf.size
  2301.         rep movsb
  2302.         ret
  2303. .addr_error:    ; if given memory address is illegal
  2304.         mov     dword [esp + SYSCALL_STACK.eax], -1
  2305.         ret  
  2306. ;------------------------------------------------------------------------------
  2307. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2308.         ;wait retrace functions
  2309.         mov     edx, 0x3da
  2310. .loop:
  2311.         in      al, dx
  2312.         test    al, 1000b
  2313.         jz      .loop
  2314.         and     [esp + SYSCALL_STACK.eax], dword 0
  2315.         ret
  2316. ;------------------------------------------------------------------------------
  2317. align 4
  2318. sysfn_centermouse:      ; 18.15 = mouse centered
  2319.         mov     eax, [_display.width]
  2320.         shr     eax, 1
  2321.         mov     [MOUSE_X], ax
  2322.         mov     eax, [_display.height]
  2323.         shr     eax, 1
  2324.         mov     [MOUSE_Y], ax
  2325.         call    wakeup_osloop
  2326.         xor     eax, eax
  2327.         and     [esp + SYSCALL_STACK.eax], eax
  2328.         ret
  2329. ;------------------------------------------------------------------------------
  2330. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2331.         cmp     ecx, 8
  2332.         jnc     @f
  2333.         jmp     dword [.table + ecx*4]
  2334. .get_mouse_acceleration:
  2335.         xor     eax, eax
  2336.         mov     ax, [mouse_speed_factor]
  2337.         mov     [esp + SYSCALL_STACK.eax], eax
  2338.         ret
  2339. .set_mouse_acceleration:
  2340.         mov     [mouse_speed_factor], dx
  2341.         ret
  2342. .get_mouse_delay:
  2343.         xor     eax, eax
  2344.         mov     al, [mouse_delay]
  2345.         mov     [esp + SYSCALL_STACK.eax], eax
  2346.         ret
  2347. .set_mouse_delay:
  2348.         mov     [mouse_delay], dl
  2349. @@:
  2350.         ret
  2351. .set_pointer_position:
  2352.         cmp     dx, word[_display.height]
  2353.         jae     @b
  2354.         rol     edx, 16
  2355.         cmp     dx, word[_display.width]
  2356.         jae     @b
  2357.         mov     [MOUSE_X], edx
  2358.         mov     [mouse_active], 1
  2359.         jmp     wakeup_osloop
  2360. .set_mouse_button:
  2361.         mov     [BTN_DOWN], edx
  2362.         mov     [mouse_active], 1
  2363.         jmp     wakeup_osloop
  2364. .get_doubleclick_delay:
  2365.         xor     eax, eax
  2366.         mov     al, [mouse_doubleclick_delay]
  2367.         mov     [esp + SYSCALL_STACK.eax], eax
  2368.         ret
  2369. .set_doubleclick_delay:
  2370.         mov     [mouse_doubleclick_delay], dl
  2371.         ret
  2372. align 4
  2373. .table:
  2374. dd      .get_mouse_acceleration
  2375. dd      .set_mouse_acceleration
  2376. dd      .get_mouse_delay
  2377. dd      .set_mouse_delay
  2378. dd      .set_pointer_position
  2379. dd      .set_mouse_button
  2380. dd      .get_doubleclick_delay
  2381. dd      .set_doubleclick_delay
  2382. ;------------------------------------------------------------------------------
  2383. sysfn_getfreemem:
  2384.         mov     eax, [pg_data.pages_free]
  2385.         shl     eax, 2
  2386.         mov     [esp + SYSCALL_STACK.eax], eax
  2387.         ret
  2388. ;------------------------------------------------------------------------------
  2389. sysfn_getallmem:
  2390.         mov     eax, [MEM_AMOUNT]
  2391.         shr     eax, 10
  2392.         mov     [esp + SYSCALL_STACK.eax], eax
  2393.         ret
  2394. ;------------------------------------------------------------------------------
  2395. sysfn_pid_to_slot:
  2396.         mov     eax, ecx
  2397.         call    pid_to_slot
  2398.         mov     [esp + SYSCALL_STACK.eax], eax
  2399.         ret
  2400. ;------------------------------------------------------------------------------
  2401. sysfn_min_rest_window:
  2402.         pushad
  2403.         mov     eax, edx ; ebx - operating
  2404.         shr     ecx, 1
  2405.         jnc     @f
  2406.         call    pid_to_slot
  2407. @@:
  2408.         or      eax, eax ; eax - number of slot
  2409.         jz      .error
  2410.         cmp     eax, max_processes    ; varify maximal slot number
  2411.         ja      .error
  2412.         movzx   eax, word [WIN_STACK + eax*2]
  2413.         shr     ecx, 1
  2414.         jc      .restore
  2415.  ; .minimize:
  2416.         call    minimize_window
  2417.         jmp     .exit
  2418. .restore:
  2419.         call    restore_minimized_window
  2420. .exit:
  2421.         popad
  2422.         xor     eax, eax
  2423.         mov     [esp + SYSCALL_STACK.eax], eax
  2424.         ret
  2425. .error:
  2426.         popad
  2427.         xor     eax, eax
  2428.         dec     eax
  2429.         mov     [esp + SYSCALL_STACK.eax], eax
  2430.         ret
  2431. ;------------------------------------------------------------------------------
  2432. sysfn_min_windows:
  2433.         call    minimize_all_window
  2434.         mov     [esp + SYSCALL_STACK.eax], eax
  2435.         call    change_task
  2436.         ret
  2437. ;------------------------------------------------------------------------------
  2438. sysfn_set_screen_sizes:
  2439.         cmp     [SCR_MODE], word 0x13
  2440.         jbe     .exit
  2441.  
  2442.         cmp     [_display.select_cursor], select_cursor
  2443.         jne     .exit
  2444.  
  2445.         cmp     ecx, [display_width_standard]
  2446.         ja      .exit
  2447.  
  2448.         cmp     edx, [display_height_standard]
  2449.         ja      .exit
  2450.  
  2451.         pushfd
  2452.         cli
  2453.         mov     eax, ecx
  2454.         mov     ecx, [_display.lfb_pitch]
  2455.         mov     [_display.width], eax
  2456.         dec     eax
  2457.         mov     [_display.height], edx
  2458.         dec     edx
  2459. ; eax - new Screen_Max_X
  2460. ; edx - new Screen_Max_Y
  2461.         mov     [do_not_touch_winmap], 1
  2462.         call    set_screen
  2463.         mov     [do_not_touch_winmap], 0
  2464.         popfd
  2465.         call    change_task
  2466. .exit:
  2467.         ret
  2468. ;------------------------------------------------------------------------------
  2469. uglobal
  2470. screen_workarea RECT
  2471. display_width_standard dd 0
  2472. display_height_standard dd 0
  2473. do_not_touch_winmap db 0
  2474. window_minimize db 0
  2475. sound_flag      db 0
  2476.  
  2477. endg
  2478.  
  2479. iglobal
  2480. version_inf:
  2481.         db 0,7,7,0  ; version 0.7.7.0
  2482.         db 0
  2483. .rev    dd __REV__
  2484. .size = $ - version_inf
  2485. endg
  2486. ;------------------------------------------------------------------------------
  2487. align 4
  2488. sys_cachetodiskette:
  2489.         cmp     ebx, 1
  2490.         jb      .no_floppy_save
  2491.         cmp     ebx, 2
  2492.         ja      .no_floppy_save
  2493.         call    save_image
  2494.         mov     [esp + SYSCALL_STACK.eax], eax
  2495.         ret
  2496. .no_floppy_save:
  2497.         mov     [esp + SYSCALL_STACK.eax], dword 1
  2498.         ret
  2499. ;------------------------------------------------------------------------------
  2500. align 4
  2501. sys_cpuusage:
  2502.  
  2503. ;  RETURN:
  2504. ;
  2505. ;  +00 dword     process cpu usage
  2506. ;  +04  word     position in windowing stack
  2507. ;  +06  word     windowing stack value at current position (cpu nro)
  2508. ;  +10 12 bytes  name
  2509. ;  +22 dword     start in mem
  2510. ;  +26 dword     used mem
  2511. ;  +30 dword     PID , process idenfification number
  2512. ;
  2513.         ; if given memory address belongs to kernel then error
  2514.         stdcall is_region_userspace, ebx, 0x4C
  2515.         jnz     .addr_error
  2516.  
  2517.         cmp     ecx, -1 ; who am I ?
  2518.         jne     .no_who_am_i
  2519.         mov     ecx, [current_slot_idx]
  2520.   .no_who_am_i:
  2521.         cmp     ecx, max_processes
  2522.         ja      .nofillbuf
  2523.  
  2524. ; +4: word: position of the window of thread in the window stack
  2525.         mov     ax, [WIN_STACK + ecx * 2]
  2526.         mov     [ebx+4], ax
  2527. ; +6: word: number of the thread slot, which window has in the window stack
  2528. ;           position ecx (has no relation to the specific thread)
  2529.         mov     ax, [WIN_POS + ecx * 2]
  2530.         mov     [ebx+6], ax
  2531.  
  2532.         shl     ecx, BSF sizeof.APPDATA
  2533.  
  2534. ; +0: dword: memory usage
  2535.         ;mov     eax, [ecx+TASK_TABLE+TASKDATA.cpu_usage]
  2536.         mov     eax, [SLOT_BASE + ecx - sizeof.APPDATA + APPDATA.cpu_usage]
  2537.         mov     [ebx], eax
  2538. ; +10: 11 bytes: name of the process
  2539.         push    ecx
  2540.         lea     eax, [SLOT_BASE + ecx + APPDATA.app_name]
  2541.         add     ebx, 10
  2542.         mov     ecx, 11
  2543.         call    memmove
  2544.         pop     ecx
  2545.  
  2546.         shr     ecx, (BSF sizeof.APPDATA - BSF sizeof.WDATA)
  2547. ; +22: address of the process in memory
  2548. ; +26: size of used memory - 1
  2549.         push    edi
  2550.         lea     edi, [ebx+12]
  2551.         xor     eax, eax
  2552.         mov     edx, 0x100000*16
  2553.         cmp     ecx, 1 shl BSF sizeof.WDATA
  2554.         je      .os_mem
  2555.         mov     edx, [SLOT_BASE + ecx*(sizeof.APPDATA/sizeof.WDATA) + APPDATA.process]
  2556.         mov     edx, [edx + PROC.mem_used]
  2557.         mov     eax, std_application_base_address
  2558. .os_mem:
  2559.         stosd
  2560.         lea     eax, [edx-1]
  2561.         stosd
  2562.  
  2563. ; +30: PID/TID
  2564.         mov     eax, [SLOT_BASE + ecx*(sizeof.APPDATA/sizeof.WDATA) + APPDATA.tid]
  2565.         stosd
  2566.  
  2567.     ; window position and size
  2568.         push    esi
  2569.         lea     esi, [window_data + ecx + WDATA.box]
  2570.         movsd
  2571.         movsd
  2572.         movsd
  2573.         movsd
  2574.  
  2575.     ; Process state (+50)
  2576.         movzx   eax, byte [SLOT_BASE + ecx*(sizeof.APPDATA/sizeof.WDATA) + APPDATA.state]
  2577.         stosd
  2578.  
  2579.     ; Window client area box
  2580.         lea     esi, [window_data + ecx + WDATA.clientbox]
  2581.         movsd
  2582.         movsd
  2583.         movsd
  2584.         movsd
  2585.  
  2586.     ; Window state
  2587.         mov     al, [window_data + ecx + WDATA.fl_wstate]
  2588.         stosb
  2589.  
  2590.     ; Event mask (+71)
  2591.         mov     EAX, dword [SLOT_BASE + ecx*(sizeof.APPDATA/sizeof.WDATA) + APPDATA.event_mask]
  2592.         stosd
  2593.  
  2594.     ; Keyboard mode (+75)
  2595.         mov     al, byte [SLOT_BASE + ecx*(sizeof.APPDATA/sizeof.WDATA) + APPDATA.keyboard_mode]
  2596.         stosb
  2597.  
  2598.         pop     esi
  2599.         pop     edi
  2600.  
  2601. .nofillbuf:
  2602.     ; return number of processes
  2603.  
  2604.         mov     eax, [thread_count]
  2605.         mov     [esp + SYSCALL_STACK.eax], eax
  2606.         ret
  2607.  
  2608. .addr_error:    ; if given memory address is illegal
  2609.         mov     dword [esp + SYSCALL_STACK.eax], -1
  2610.         ret  
  2611.  
  2612.  
  2613. ; redraw status
  2614. align 4
  2615. sys_redrawstat:
  2616.         cmp     ebx, 1
  2617.         jne     .no_widgets_away
  2618.         ; buttons away
  2619.         mov     ecx, [current_slot_idx]
  2620. .sys_newba2:
  2621.         mov     edi, [BTN_ADDR]
  2622.         cmp     [edi], dword 0  ; empty button list ?
  2623.         je      .end_of_buttons_away
  2624.         movzx   ebx, word [edi]
  2625.         inc     ebx
  2626.         mov     eax, edi
  2627. .sys_newba:
  2628.         dec     ebx
  2629.         jz      .end_of_buttons_away
  2630.  
  2631.         add     eax, 0x10
  2632.         cmp     cx, [eax]
  2633.         jnz     .sys_newba
  2634.  
  2635.         push    eax ebx ecx
  2636.         mov     ecx, ebx
  2637.         inc     ecx
  2638.         shl     ecx, 4
  2639.         mov     ebx, eax
  2640.         add     eax, 0x10
  2641.         call    memmove
  2642.         dec     dword [edi]
  2643.         pop     ecx ebx eax
  2644.  
  2645.         jmp     .sys_newba2
  2646.  
  2647. .end_of_buttons_away:
  2648.         ret
  2649.  
  2650. .no_widgets_away:
  2651.  
  2652.         cmp     ebx, 2
  2653.         jnz     .srl1
  2654.  
  2655.         mov     edx, [current_slot]      ; return whole screen draw area for this app
  2656.         mov     edx, [edx + APPDATA.window]
  2657.         mov     [edx + WDATA.draw_data.left], 0
  2658.         mov     [edx + WDATA.draw_data.top], 0
  2659.         mov     eax, [_display.width]
  2660.         dec     eax
  2661.         mov     [edx + WDATA.draw_data.right], eax
  2662.         mov     eax, [_display.height]
  2663.         dec     eax
  2664.         mov     [edx + WDATA.draw_data.bottom], eax
  2665.  
  2666. .srl1:
  2667.         ret
  2668.  
  2669. ;ok - 100% work
  2670. ;nt - not tested
  2671. ;---------------------------------------------------------------------------------------------
  2672. ;eax
  2673. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2674. ;1 - change task. Ret nothing. Block. ok.
  2675. ;2 - performance control
  2676. ; ebx
  2677. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2678. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2679. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2680. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2681. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2682. ;eax
  2683. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2684. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2685. ;---------------------------------------------------------------------------------------------
  2686. iglobal
  2687. align 4
  2688. sheduler:
  2689.         dd      sys_sheduler.00
  2690.         dd      change_task
  2691.         dd      sys_sheduler.02
  2692.         dd      sys_sheduler.03
  2693.         dd      sys_sheduler.04
  2694. endg
  2695. sys_sheduler:
  2696. ;rewritten by <Lrz>  29.12.2009
  2697.         jmp     dword [sheduler + ebx*4]
  2698. ;.shed_counter:
  2699. .00:
  2700.         mov     eax, [context_counter]
  2701.         mov     [esp + SYSCALL_STACK.eax], eax
  2702.         ret
  2703.  
  2704. .02:
  2705. ;.perf_control:
  2706.         inc     ebx                     ;before ebx=2, ebx=3
  2707.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  2708.         jz      cache_disable
  2709.  
  2710.         dec     ebx                     ;ebx=2
  2711.         cmp     ebx, ecx                ;
  2712.         jz      cache_enable            ;if ecx=2 and ebx=2
  2713.  
  2714.         dec     ebx                     ;ebx=1
  2715.         cmp     ebx, ecx
  2716.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  2717.  
  2718.         dec     ebx
  2719.         test    ebx, ecx                ;ebx=0 and ecx=0
  2720.         jz      modify_pce              ;if ecx=0
  2721.  
  2722.         ret
  2723.  
  2724. .03:
  2725. ;.rdmsr_instr:
  2726. ;now counter in ecx
  2727. ;(edx:eax) esi:edi => edx:esi
  2728.         mov     eax, esi
  2729.         mov     ecx, edx
  2730.         rdmsr
  2731.         mov     [esp + SYSCALL_STACK.eax], eax
  2732.         mov     [esp + SYSCALL_STACK.ebx], edx           ;ret in ebx?
  2733.         ret
  2734.  
  2735. .04:
  2736. ;.wrmsr_instr:
  2737. ;now counter in ecx
  2738. ;(edx:eax) esi:edi => edx:esi
  2739.         ; Fast Call MSR can't be destroy
  2740.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  2741.         ; включаются/выключаются расширенные возможности
  2742.         cmp     edx, MSR_SYSENTER_CS
  2743.         je      @f
  2744.         cmp     edx, MSR_SYSENTER_ESP
  2745.         je      @f
  2746.         cmp     edx, MSR_SYSENTER_EIP
  2747.         je      @f
  2748.         cmp     edx, MSR_AMD_STAR
  2749.         je      @f
  2750.  
  2751.         mov     eax, esi
  2752.         mov     ecx, edx
  2753.         wrmsr
  2754.         ; mov   [esp + 32], eax
  2755.         ; mov   [esp + 20], edx ;ret in ebx?
  2756. @@:
  2757.         ret
  2758.  
  2759. cache_disable:
  2760.         mov     eax, cr0
  2761.         or      eax, 01100000_00000000_00000000_00000000b
  2762.         mov     cr0, eax
  2763.         wbinvd  ;set MESI
  2764.         ret
  2765.  
  2766. cache_enable:
  2767.         mov     eax, cr0
  2768.         and     eax, 10011111_11111111_11111111_11111111b
  2769.         mov     cr0, eax
  2770.         ret
  2771.  
  2772. is_cache_enabled:
  2773.         mov     eax, cr0
  2774.         mov     ebx, eax
  2775.         and     eax, 01100000_00000000_00000000_00000000b
  2776.         jz      cache_disabled
  2777.         mov     [esp + SYSCALL_STACK.eax], ebx
  2778. cache_disabled:
  2779.         mov     dword [esp + SYSCALL_STACK.eax], eax;0
  2780.         ret
  2781.  
  2782. modify_pce:
  2783.         mov     eax, cr4
  2784. ;       mov ebx,0
  2785. ;       or  bx,100000000b ;pce
  2786. ;       xor eax,ebx ;invert pce
  2787.         bts     eax, 8;pce=cr4[8]
  2788.         mov     cr4, eax
  2789.         mov     [esp + SYSCALL_STACK.eax], eax
  2790.         ret
  2791. ;---------------------------------------------------------------------------------------------
  2792.  
  2793.  
  2794. iglobal
  2795.   cpustring db 'CPU',0
  2796. endg
  2797.  
  2798. uglobal
  2799. background_defined    db    0    ; diamond, 11.04.2006
  2800. endg
  2801. ;-----------------------------------------------------------------------------
  2802. align 4
  2803. checkmisc:
  2804.         cmp     [ctrl_alt_del], 1
  2805.         jne     .nocpustart
  2806.  
  2807.         mov     ebp, cpustring
  2808.         call    fs_execute_from_sysdir
  2809.  
  2810.         mov     [ctrl_alt_del], 0
  2811. ;--------------------------------------
  2812. align 4
  2813. .nocpustart:
  2814.         cmp     [mouse_active], 1
  2815.         jne     .mouse_not_active
  2816.         mov     [mouse_active], 0
  2817.  
  2818.         xor     edi, edi
  2819.         mov     ebx, window_data
  2820.  
  2821.         mov     ecx, [thread_count]
  2822.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  2823.         shl     eax, BSF sizeof.APPDATA
  2824.         push    eax
  2825.  
  2826.         movzx   eax, word [MOUSE_X]
  2827.         movzx   edx, word [MOUSE_Y]
  2828. ;--------------------------------------
  2829. align 4
  2830. .set_mouse_event:
  2831.         add     edi, sizeof.APPDATA
  2832.         add     ebx, sizeof.WDATA
  2833.         test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_MOUSE_FILTER
  2834.         jz      .pos_filter
  2835.  
  2836.         cmp     edi, [esp]                      ; skip if filtration active
  2837.         jne     .skip
  2838. ;--------------------------------------
  2839. align 4
  2840. .pos_filter:
  2841.         test    [SLOT_BASE + edi + APPDATA.event_mask], EVM_CURSOR_FILTER
  2842.         jz      .set
  2843.  
  2844.         mov     esi, [ebx + WDATA.box.left]
  2845.         cmp     eax, esi
  2846.         jb      .skip
  2847.         add     esi, [ebx + WDATA.box.width]
  2848.         cmp     eax, esi
  2849.         ja      .skip
  2850.  
  2851.         mov     esi, [ebx + WDATA.box.top]
  2852.         cmp     edx, esi
  2853.         jb      .skip
  2854.         add     esi, [ebx + WDATA.box.height]
  2855.         cmp     edx, esi
  2856.         ja      .skip
  2857. ;--------------------------------------
  2858. align 4
  2859. .set:
  2860.         or      [SLOT_BASE + edi + APPDATA.occurred_events], EVENT_MOUSE
  2861. ;--------------------------------------
  2862. align 4
  2863. .skip:
  2864.         loop    .set_mouse_event
  2865.  
  2866.         pop     eax
  2867. ;--------------------------------------
  2868. align 4
  2869. .mouse_not_active:
  2870.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  2871.         jz      nobackgr
  2872.  
  2873.         cmp     [background_defined], 0
  2874.         jz      nobackgr
  2875. ;--------------------------------------
  2876. align 4
  2877. backgr:
  2878.         mov     eax, [background_window + WDATA.draw_data.left]
  2879.         shl     eax, 16
  2880.         add     eax, [background_window + WDATA.draw_data.right]
  2881.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  2882.  
  2883.         mov     eax, [background_window + WDATA.draw_data.top]
  2884.         shl     eax, 16
  2885.         add     eax, [background_window + WDATA.draw_data.bottom]
  2886.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  2887.  
  2888.         call    drawbackground
  2889. ;        DEBUGF  1, "K : drawbackground\n"
  2890. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  2891. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  2892. ;--------- set event 5 start ----------
  2893.         push    ecx edi
  2894.         xor     edi, edi
  2895.         mov     ecx, [thread_count]
  2896. ;--------------------------------------
  2897. align 4
  2898. set_bgr_event:
  2899.         add     edi, sizeof.WDATA
  2900.         mov     eax, [BG_Rect_X_left_right]
  2901.         mov     edx, [BG_Rect_Y_top_bottom]
  2902.         cmp     [window_data + edi + WDATA.draw_bgr_x], 0
  2903.         jz      .set
  2904. .join:
  2905.         cmp     word [window_data + edi + WDATA.draw_bgr_x], ax
  2906.         jae     @f
  2907.         mov     word [window_data + edi + WDATA.draw_bgr_x], ax
  2908. @@:
  2909.         shr     eax, 16
  2910.         cmp     word [window_data + edi + WDATA.draw_bgr_x + 2], ax
  2911.         jbe     @f
  2912.         mov     word [window_data + edi + WDATA.draw_bgr_x + 2], ax
  2913. @@:
  2914.         cmp     word [window_data + edi + WDATA.draw_bgr_y], dx
  2915.         jae     @f
  2916.         mov     word [window_data + edi + WDATA.draw_bgr_y], dx
  2917. @@:
  2918.         shr     edx, 16
  2919.         cmp     word [window_data + edi + WDATA.draw_bgr_y+2], dx
  2920.         jbe     @f
  2921.         mov     word [window_data + edi + WDATA.draw_bgr_y+2], dx
  2922. @@:
  2923.         jmp     .common
  2924. .set:
  2925.         mov     [window_data + edi + WDATA.draw_bgr_x], eax
  2926.         mov     [window_data + edi + WDATA.draw_bgr_y], edx
  2927. .common:
  2928.         mov     eax, [window_data + edi + WDATA.thread]
  2929.         test    eax, eax
  2930.         jz      @f
  2931.         or      [eax + APPDATA.occurred_events], EVENT_BACKGROUND
  2932. @@:
  2933.         sub     ecx, 1
  2934.         jnz     set_bgr_event
  2935.         ;loop    set_bgr_event
  2936.         pop     edi ecx
  2937. ;--------- set event 5 stop -----------
  2938.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  2939.         jnz     backgr
  2940.  
  2941.         xor     eax, eax
  2942.         mov     [background_window + WDATA.draw_data.left], eax
  2943.         mov     [background_window + WDATA.draw_data.top], eax
  2944.         mov     [background_window + WDATA.draw_data.right], eax
  2945.         mov     [background_window + WDATA.draw_data.bottom], eax
  2946. ;--------------------------------------
  2947. align 4
  2948. nobackgr:
  2949. ; system shutdown request
  2950.         cmp     [SYS_SHUTDOWN], byte 0
  2951.         je      noshutdown
  2952.  
  2953.         mov     edx, [shutdown_processes]
  2954.  
  2955.         cmp     [SYS_SHUTDOWN], dl
  2956.         jne     noshutdown
  2957.  
  2958.         lea     ecx, [edx-1]
  2959.         mov     edx, SLOT_BASE + sizeof.APPDATA ;OS_BASE+0x3040
  2960.         jecxz   no_mark_system_shutdown
  2961. ;--------------------------------------
  2962. align 4
  2963. markz:
  2964.         push    ecx edx
  2965.         cmp     [edx + APPDATA.state], TSTATE_FREE
  2966.         jz      .nokill
  2967.         cmp     [edx + APPDATA.process], sys_proc
  2968.         jz      .nokill
  2969.         call    request_terminate
  2970.         jmp     .common
  2971. .nokill:
  2972.         dec     byte [SYS_SHUTDOWN]
  2973.         xor     eax, eax
  2974. .common:
  2975.         pop     edx ecx
  2976.         test    eax, eax
  2977.         jz      @f
  2978.         mov     [edx + APPDATA.state], TSTATE_ZOMBIE
  2979. @@:
  2980.         add     edx, sizeof.APPDATA
  2981.         loop    markz
  2982.         call    wakeup_osloop
  2983. ;--------------------------------------
  2984. align 4
  2985. @@:
  2986. no_mark_system_shutdown:
  2987.         dec     byte [SYS_SHUTDOWN]
  2988.         je      system_shutdown
  2989. ;--------------------------------------
  2990. align 4
  2991. noshutdown:
  2992.         mov     eax, [thread_count]           ; termination
  2993.         mov     ebx, SLOT_BASE + sizeof.APPDATA + APPDATA.state
  2994.         mov     esi, 1
  2995. ;--------------------------------------
  2996. align 4
  2997. newct:
  2998.         mov     cl, [ebx]
  2999.         cmp     cl, TSTATE_ZOMBIE
  3000.         jz      .terminate
  3001.  
  3002.         cmp     cl, TSTATE_TERMINATING
  3003.         jnz     .noterminate
  3004. .terminate:
  3005.         pushad
  3006.         mov     ecx, eax
  3007.         shl     ecx, BSF sizeof.WDATA
  3008.         add     ecx, window_data
  3009.         call    restore_default_cursor_before_killing
  3010.         popad
  3011.  
  3012.         pushad
  3013.         call    terminate
  3014.         popad
  3015.         cmp     byte [SYS_SHUTDOWN], 0
  3016.         jz      .noterminate
  3017.         dec     byte [SYS_SHUTDOWN]
  3018.         je      system_shutdown
  3019.  
  3020. .noterminate:
  3021.         add     ebx, sizeof.APPDATA
  3022.         inc     esi
  3023.         dec     eax
  3024.         jnz     newct
  3025.         ret
  3026. ;-----------------------------------------------------------------------------
  3027. align 4
  3028. redrawscreen:
  3029. ; eax , if process window_data base is eax, do not set flag/limits
  3030.  
  3031.         pushad
  3032.         push    eax
  3033.  
  3034. ;;;         mov   ebx,2
  3035. ;;;         call  delay_hs
  3036.  
  3037.          ;mov   ecx,0               ; redraw flags for apps
  3038.         xor     ecx, ecx
  3039. ;--------------------------------------
  3040. align 4
  3041. newdw2:
  3042.         inc     ecx
  3043.         push    ecx
  3044.  
  3045.         mov     eax, ecx
  3046.         shl     eax, BSF sizeof.WDATA
  3047.         add     eax, window_data
  3048.  
  3049.         cmp     eax, [esp+4]
  3050.         je      not_this_task
  3051.                                    ; check if window in redraw area
  3052.         mov     edi, eax
  3053.  
  3054.         cmp     ecx, 1             ; limit for background
  3055.         jz      bgli
  3056.  
  3057.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  3058.         test    eax, eax
  3059.         jz      @f
  3060.         mov     al, [eax + WDATA.z_modif]
  3061.         cmp     [edi + WDATA.z_modif], al
  3062.         jg      ricino
  3063.       @@:
  3064.  
  3065.         mov     eax, [edi + WDATA.box.left]
  3066.         mov     ebx, [edi + WDATA.box.top]
  3067.  
  3068.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3069.         cmp     ecx, ebx
  3070.         jb      ricino
  3071.  
  3072.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3073.         cmp     ecx, eax
  3074.         jb      ricino
  3075.  
  3076.         mov     eax, [edi + WDATA.box.left]
  3077.         mov     ebx, [edi + WDATA.box.top]
  3078.         mov     ecx, [edi + WDATA.box.width]
  3079.         mov     edx, [edi + WDATA.box.height]
  3080.         add     ecx, eax
  3081.         add     edx, ebx
  3082.  
  3083.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3084.         cmp     edx, eax
  3085.         jb      ricino
  3086.  
  3087.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3088.         cmp     ecx, eax
  3089.         jb      ricino
  3090. ;--------------------------------------
  3091. align 4
  3092. bgli:
  3093.         cmp     dword[esp], 1
  3094.         jnz     .az
  3095.  
  3096.         cmp     [REDRAW_BACKGROUND], 0
  3097.         jz      .az
  3098.  
  3099.         mov     dl, 0
  3100.         mov     ebx, [draw_limits.left]
  3101.         cmp     ebx, [edi + WDATA.draw_data.left]
  3102.         jae     @f
  3103.  
  3104.         mov     [edi + WDATA.draw_data.left], ebx
  3105.         mov     dl, 1
  3106. ;--------------------------------------
  3107. align 4
  3108. @@:
  3109.         mov     ebx, [draw_limits.top]
  3110.         cmp     ebx, [edi + WDATA.draw_data.top]
  3111.         jae     @f
  3112.  
  3113.         mov     [edi + WDATA.draw_data.top], ebx
  3114.         mov     dl, 1
  3115. ;--------------------------------------
  3116. align 4
  3117. @@:
  3118.         mov     ebx, [draw_limits.right]
  3119.         cmp     ebx, [edi + WDATA.draw_data.right]
  3120.         jbe     @f
  3121.  
  3122.         mov     [edi + WDATA.draw_data.right], ebx
  3123.         mov     dl, 1
  3124. ;--------------------------------------
  3125. align 4
  3126. @@:
  3127.         mov     ebx, [draw_limits.bottom]
  3128.         cmp     ebx, [edi + WDATA.draw_data.bottom]
  3129.         jbe     @f
  3130.  
  3131.         mov     [edi + WDATA.draw_data.bottom], ebx
  3132.         mov     dl, 1
  3133. ;--------------------------------------
  3134. align 4
  3135. @@:
  3136.         add     [REDRAW_BACKGROUND], dl
  3137.         call    wakeup_osloop
  3138.         jmp     newdw8
  3139. ;--------------------------------------
  3140. align 4
  3141. .az:
  3142.         mov     eax, edi
  3143.  
  3144.         mov     ebx, [draw_limits.left]        ; set limits
  3145.         mov     [eax + WDATA.draw_data.left], ebx
  3146.         mov     ebx, [draw_limits.top]
  3147.         mov     [eax + WDATA.draw_data.top], ebx
  3148.         mov     ebx, [draw_limits.right]
  3149.         mov     [eax + WDATA.draw_data.right], ebx
  3150.         mov     ebx, [draw_limits.bottom]
  3151.         mov     [eax + WDATA.draw_data.bottom], ebx
  3152.  
  3153.         cmp     dword [esp], 1
  3154.         jne     nobgrd
  3155.         inc     [REDRAW_BACKGROUND]
  3156.         call    wakeup_osloop
  3157. ;--------------------------------------
  3158. align 4
  3159. newdw8:
  3160. nobgrd:
  3161. ;--------------------------------------
  3162.         push    eax  edi ebp
  3163.         mov     edi, [esp+12]
  3164.         cmp     edi, 1
  3165.         je      .found
  3166.  
  3167.         mov     eax, [draw_limits.left]
  3168.         mov     ebx, [draw_limits.top]
  3169.         mov     ecx, [draw_limits.right]
  3170.         sub     ecx, eax
  3171.         test    ecx, ecx
  3172.         jz      .not_found
  3173.  
  3174.         mov     edx, [draw_limits.bottom]
  3175.         sub     edx, ebx
  3176.         test    edx, edx
  3177.         jz      .not_found
  3178.  
  3179. ; eax - x, ebx - y
  3180. ; ecx - size x, edx - size y
  3181.         add     ebx, edx
  3182. ;--------------------------------------
  3183. align 4
  3184. .start_y:
  3185.         push    ecx
  3186. ;--------------------------------------
  3187. align 4
  3188. .start_x:
  3189.         add     eax, ecx
  3190.         mov     ebp, [d_width_calc_area + ebx*4]
  3191.         add     ebp, [_display.win_map]
  3192.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3193.         cmp     ebp, edi
  3194.         jne     @f
  3195.  
  3196.         pop     ecx
  3197.         jmp     .found
  3198. ;--------------------------------------
  3199. align 4
  3200. @@:
  3201.         sub     eax, ecx
  3202.  
  3203.         dec     ecx
  3204.         jns     .start_x
  3205.  
  3206.         pop     ecx
  3207.         dec     ebx
  3208.         dec     edx
  3209.         jns     .start_y
  3210. ;--------------------------------------
  3211. align 4
  3212. .not_found:
  3213.         pop     ebp edi eax
  3214.         jmp     ricino
  3215. ;--------------------------------------
  3216. align 4
  3217. .found:
  3218.         pop     ebp edi eax
  3219.  
  3220.         mov     [eax + WDATA.fl_redraw], WSTATE_REDRAW  ; mark as redraw
  3221. ;--------------------------------------
  3222. align 4
  3223. ricino:
  3224. not_this_task:
  3225.         pop     ecx
  3226.  
  3227.         cmp     ecx, [thread_count]
  3228.         jle     newdw2
  3229.  
  3230.         pop     eax
  3231.         popad
  3232.         ret
  3233. ;-----------------------------------------------------------------------------
  3234. align 4
  3235. calculatebackground:   ; background
  3236.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3237.         mov     eax, 0x01010101
  3238.         mov     ecx, [_display.win_map_size]
  3239.         shr     ecx, 2
  3240.         rep stosd
  3241.         mov     byte[background_window + WDATA.z_modif], ZPOS_DESKTOP
  3242.         mov     [REDRAW_BACKGROUND], 0
  3243.         ret
  3244. ;-----------------------------------------------------------------------------
  3245. uglobal
  3246.   imax    dd 0x0
  3247. endg
  3248. ;-----------------------------------------------------------------------------
  3249. align 4
  3250. delay_ms:     ; delay in 1/1000 sec
  3251.         pushad
  3252.  
  3253.         cmp     [hpet_base], 0
  3254.         jz      .no_hpet
  3255.         mov     eax, esi
  3256.         mov     edx, 1_000_000 ; ms to ns
  3257.         mul     edx
  3258.         mov     ebx, edx
  3259.         mov     ecx, eax
  3260.  
  3261.         push    ecx
  3262.         call    get_clock_ns
  3263.         pop     ecx
  3264.         mov     edi, edx
  3265.         mov     esi, eax
  3266. .wait:
  3267.         push    ecx
  3268.         call    get_clock_ns
  3269.         pop     ecx
  3270.         sub     eax, esi
  3271.         sbb     edx, edi
  3272.         sub     eax, ecx
  3273.         sbb     edx, ebx
  3274.         jc      .wait
  3275.         jmp     .done
  3276.  
  3277. .no_hpet:
  3278.         mov     ecx, esi
  3279.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3280.         imul    ecx, 33941
  3281.         shr     ecx, 9
  3282.         ; </CPU clock fix>
  3283.  
  3284.         in      al, 0x61
  3285.         and     al, 0x10
  3286.         mov     ah, al
  3287.         cld
  3288.  
  3289. .cnt1:
  3290.         in      al, 0x61
  3291.         and     al, 0x10
  3292.         cmp     al, ah
  3293.         jz      .cnt1
  3294.  
  3295.         mov     ah, al
  3296.         loop    .cnt1
  3297.  
  3298. .done:
  3299.         popad
  3300.         ret
  3301. ;-----------------------------------------------------------------------------
  3302. align 4
  3303. set_app_param:
  3304.         mov     edi, [current_slot]
  3305.         xchg    ebx, [edi + APPDATA.event_mask] ; set new event mask
  3306.         mov     [esp + SYSCALL_STACK.eax], ebx  ; return old mask value
  3307.         ret
  3308. ;-----------------------------------------------------------------------------
  3309.  
  3310. ; this is for syscall
  3311. proc delay_hs_unprotected
  3312.         call    unprotect_from_terminate
  3313.         call    delay_hs
  3314.         call    protect_from_terminate
  3315.         ret
  3316. endp
  3317.  
  3318. if 1
  3319. align 4
  3320. delay_hs:     ; delay in 1/100 secs
  3321. ; ebx = delay time
  3322.         pushad
  3323.         push    ebx
  3324.         xor     esi, esi
  3325.         mov     ecx, MANUAL_DESTROY
  3326.         call    create_event
  3327.         test    eax, eax
  3328.         jz      .done
  3329.  
  3330.         mov     ebx, edx
  3331.         mov     ecx, [esp]
  3332.         push    edx
  3333.         push    eax
  3334.         call    wait_event_timeout
  3335.         pop     eax
  3336.         pop     ebx
  3337.         call    destroy_event
  3338. .done:
  3339.         add     esp, 4
  3340.         popad
  3341.         ret
  3342.  
  3343. else
  3344.  
  3345. align 4
  3346. delay_hs:     ; delay in 1/100 secs
  3347. ; ebx = delay time
  3348.         push    ecx
  3349.         push    edx
  3350.  
  3351.         mov     edx, [timer_ticks]
  3352. ;--------------------------------------
  3353. align 4
  3354. .newtic:
  3355.         mov     ecx, [timer_ticks]
  3356.         sub     ecx, edx
  3357.         cmp     ecx, ebx
  3358.         jae     .zerodelay
  3359.  
  3360.         call    change_task
  3361.  
  3362.         jmp     .newtic
  3363. ;--------------------------------------
  3364. align 4
  3365. .zerodelay:
  3366.         pop     edx
  3367.         pop     ecx
  3368.         ret
  3369. end if
  3370.  
  3371. ;-----------------------------------------------------------------------------
  3372. align 16        ;very often call this subrutine
  3373. memmove:       ; memory move in bytes
  3374. ; eax = from
  3375. ; ebx = to
  3376. ; ecx = no of bytes
  3377.         test    ecx, ecx
  3378.         jle     .ret
  3379.  
  3380.         push    esi edi ecx
  3381.  
  3382.         mov     edi, ebx
  3383.         mov     esi, eax
  3384.  
  3385.         test    ecx, not 11b
  3386.         jz      @f
  3387.  
  3388.         push    ecx
  3389.         shr     ecx, 2
  3390.         rep movsd
  3391.         pop     ecx
  3392.         and     ecx, 11b
  3393.         jz      .finish
  3394. ;--------------------------------------
  3395. align 4
  3396. @@:
  3397.         rep movsb
  3398. ;--------------------------------------
  3399. align 4
  3400. .finish:
  3401.         pop     ecx edi esi
  3402. ;--------------------------------------
  3403. align 4
  3404. .ret:
  3405.         ret
  3406. ;-----------------------------------------------------------------------------
  3407.  
  3408. ; in: eax = port
  3409. ;     ebp = subfunction
  3410. ;          0 - set access
  3411. ;          1 - clear access
  3412. ; out: not return value
  3413. align 4
  3414. set_io_access_rights:
  3415.         push    edi eax
  3416.         mov     edi, tss._io_map_0
  3417.  
  3418.         test    ebp, ebp         ; enable access - ebp = 0
  3419.         jnz     .siar1
  3420.  
  3421.         btr     [edi], eax
  3422.         pop     eax edi
  3423.         ret
  3424. .siar1:
  3425.         bts     [edi], eax      ; disable access - ebp = 1
  3426.         pop     eax edi
  3427.         ret
  3428.  
  3429. align 4
  3430. ; @brief ReservePortArea and FreePortArea
  3431. ; @param edx number end arrea of ports (include last number of port)
  3432. ; @param ecx number start arrea of ports
  3433. ; @param ebx sub function 0 - reserve, 1 - free
  3434. ; @param eax 46 - number function
  3435. ; @returns  eax = 0 - succesful eax = 1 - error
  3436. syscall_reserveportarea:        ; ReservePortArea and FreePortArea
  3437.  
  3438.         call    r_f_port_area
  3439.         mov     [esp + SYSCALL_STACK.eax], eax
  3440.         ret
  3441.  
  3442. ;reserve/free group of ports
  3443. ;  * eax = 46 - number function
  3444. ;  * ebx = 0 - reserve, 1 - free
  3445. ;  * ecx = number start arrea of ports
  3446. ;  * edx = number end arrea of ports (include last number of port)
  3447. ;Return value:
  3448. ;  * eax = 0 - succesful
  3449. ;  * eax = 1 - error
  3450. ;  * The system has reserve this ports:
  3451. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3452. ;destroys eax,ebx, ebp
  3453. r_f_port_area:
  3454.  
  3455.         test    ebx, ebx
  3456.         jnz     .free_port_area
  3457.  
  3458.         cmp     ecx, edx      ; beginning > end ?
  3459.         ja      .err
  3460.         cmp     edx, 65536    ;test ebx, not 0xffff
  3461.         jae     .err
  3462.         mov     eax, [RESERVED_PORTS]
  3463.         test    eax, eax      ; no reserved areas ?
  3464.         je      .rpal2
  3465.         cmp     eax, 255      ; max reserved
  3466.         jae     .err
  3467.  .rpal3:
  3468.         mov     ebx, eax
  3469.         shl     ebx, 4   ;16 byte is sizeof item in RESERVED_PORTS table
  3470.         add     ebx, RESERVED_PORTS
  3471.         cmp     ecx, [ebx+8]
  3472.         ja      .rpal4
  3473.         cmp     edx, [ebx+4]
  3474.         jae     .err
  3475.  .rpal4:
  3476.         dec     eax
  3477.         jnz     .rpal3
  3478.         jmp     .rpal2
  3479. .err:
  3480.         xor     eax, eax
  3481.         inc     eax
  3482.         ret
  3483.  
  3484.    .rpal2:
  3485.      ; enable port access at port IO map
  3486.         pushad                        ; start enable io map
  3487.         mov     eax, ecx
  3488.         xor     ebp, ebp               ; enable - eax = port
  3489.         cli
  3490. .new_port_access:
  3491.         call    set_io_access_rights
  3492.  
  3493.         inc     eax
  3494.         cmp     eax, edx
  3495.         jbe     .new_port_access
  3496.  
  3497.         sti
  3498.         popad                         ; end enable io map
  3499.  
  3500.         mov     eax, [RESERVED_PORTS]
  3501.         inc     eax
  3502.         mov     [RESERVED_PORTS], eax
  3503.         shl     eax, 4
  3504.         add     eax, RESERVED_PORTS
  3505.         mov     ebx, [current_slot]
  3506.         mov     ebx, [ebx + APPDATA.tid]
  3507.         mov     [eax], ebx   ; tid
  3508.         mov     [eax+4], ecx ;start port
  3509.         mov     [eax+8], edx ;finish port
  3510.  
  3511.         xor     eax, eax
  3512.         ret
  3513.  
  3514. .free_port_area:
  3515.  
  3516.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3517.         test    eax, eax
  3518.         jz      .frpal2
  3519.         mov     ebx, [current_slot]
  3520.         mov     ebx, [ebx + APPDATA.tid]
  3521.    .frpal3:
  3522.         mov     edi, eax
  3523.         shl     edi, 4
  3524.         add     edi, RESERVED_PORTS
  3525.         cmp     ebx, [edi]
  3526.         jne     .frpal4
  3527.         cmp     ecx, [edi+4]
  3528.         jne     .frpal4
  3529.         cmp     edx, [edi+8]
  3530.         jne     .frpal4
  3531.         jmp     .frpal1
  3532.    .frpal4:
  3533.         dec     eax
  3534.         jnz     .frpal3
  3535.    .frpal2:
  3536.         inc     eax
  3537.         ret
  3538.    .frpal1:
  3539.         push    ecx
  3540.         mov     ecx, 256
  3541.         sub     ecx, eax
  3542.         shl     ecx, 4
  3543.         mov     esi, edi
  3544.         add     esi, 16
  3545.         cld
  3546.         rep movsb
  3547.  
  3548.         dec     dword [RESERVED_PORTS]
  3549. ;disable port access at port IO map
  3550.                         ; start disable io map
  3551.         pop     eax     ;start port
  3552.         ;cmp     edx, 65536
  3553.         ;jge     no_mask_io
  3554.  
  3555.         xor     ebp, ebp
  3556.         inc     ebp
  3557. .new_port_access_disable:           ; disable - eax = port
  3558.         call    set_io_access_rights
  3559.  
  3560.         inc     eax
  3561.         cmp     eax, edx
  3562.         jbe     .new_port_access_disable
  3563.                                     ; end disable io map
  3564.         xor     eax, eax
  3565.         ret
  3566. ;-----------------------------------------------------------------------------
  3567. align 4
  3568. drawbackground:
  3569.         cmp     [BgrDrawMode], dword 1
  3570.         jne     .bgrstr
  3571.         call    vesa20_drawbackground_tiled
  3572. ;        call    [draw_pointer]
  3573.         call    __sys_draw_pointer
  3574.         ret
  3575. ;--------------------------------------
  3576. align 4
  3577. .bgrstr:
  3578.         call    vesa20_drawbackground_stretch
  3579. ;        call    [draw_pointer]
  3580.         call    __sys_draw_pointer
  3581.         ret
  3582. ;-----------------------------------------------------------------------------
  3583. align 4
  3584. syscall_putimage:                       ; PutImage
  3585. ; add check pointer
  3586.         push    ecx
  3587.         mov     ax, cx
  3588.         shr     ecx, 16
  3589.         imul    eax, ecx
  3590.         lea     eax, [eax*3]
  3591.         stdcall is_region_userspace, ebx, eax
  3592.         pop     ecx
  3593.         jnz     sys_putimage.exit
  3594.  
  3595. sys_putimage:
  3596.         test    ecx, 0x80008000
  3597.         jnz     .exit
  3598.         test    ecx, 0x0000FFFF
  3599.         jz      .exit
  3600.         test    ecx, 0xFFFF0000
  3601.         jnz     @f
  3602. ;--------------------------------------
  3603. align 4
  3604. .exit:
  3605.         ret
  3606. ;--------------------------------------
  3607. align 4
  3608. @@:
  3609.         mov     edi, [current_slot_idx]
  3610.         shl     edi, BSF sizeof.WDATA
  3611.         add     dx, word[window_data + edi + WDATA.clientbox.top]
  3612.         rol     edx, 16
  3613.         add     dx, word[window_data + edi + WDATA.clientbox.left]
  3614.         rol     edx, 16
  3615. ;--------------------------------------
  3616. align 4
  3617. .forced:
  3618.         push    ebp esi 0
  3619.         mov     ebp, putimage_get24bpp
  3620.         mov     esi, putimage_init24bpp
  3621. ;--------------------------------------
  3622. align 4
  3623. sys_putimage_bpp:
  3624.         call    vesa20_putimage
  3625.         pop     ebp esi ebp
  3626.         ret
  3627. ;        jmp     [draw_pointer]
  3628. ;-----------------------------------------------------------------------------
  3629. align 4
  3630. sys_putimage_palette:
  3631. ; ebx = pointer to image
  3632. ; ecx = [xsize]*65536 + [ysize]
  3633. ; edx = [xstart]*65536 + [ystart]
  3634. ; esi = number of bits per pixel, must be 8, 24 or 32
  3635. ; edi = pointer to palette
  3636. ; ebp = row delta
  3637. ; check pointer
  3638.         push    ecx esi
  3639.         mov     ax, cx
  3640.         shr     ecx, 16
  3641.         imul    eax, ecx
  3642. ;        imul    eax, esi ; eax*count bit in 1 pixel
  3643. ;        shr     eax, 3
  3644.         stdcall is_region_userspace, ebx, eax
  3645.         pop     esi ecx
  3646.         jnz     sys_putimage.exit
  3647.  
  3648.         mov     eax, [current_slot_idx]
  3649.         shl     eax, BSF sizeof.WDATA
  3650.         add     dx, word [window_data + eax + WDATA.clientbox.top]
  3651.         rol     edx, 16
  3652.         add     dx, word [window_data + eax + WDATA.clientbox.left]
  3653.         rol     edx, 16
  3654. ;--------------------------------------
  3655. align 4
  3656. .forced:
  3657.         cmp     esi, 1
  3658.         jnz     @f
  3659.         push    edi
  3660.         mov     eax, [edi+4]
  3661.         sub     eax, [edi]
  3662.         push    eax
  3663.         push    dword [edi]
  3664.         push    0ffffff80h
  3665.         mov     edi, esp
  3666.         call    put_mono_image
  3667.         add     esp, 12
  3668.         pop     edi
  3669.         ret
  3670. ;--------------------------------------
  3671. align 4
  3672. @@:
  3673.         cmp     esi, 2
  3674.         jnz     @f
  3675.         push    edi
  3676.         push    0ffffff80h
  3677.         mov     edi, esp
  3678.         call    put_2bit_image
  3679.         pop     eax
  3680.         pop     edi
  3681.         ret
  3682. ;--------------------------------------
  3683. align 4
  3684. @@:
  3685.         cmp     esi, 4
  3686.         jnz     @f
  3687.         push    edi
  3688.         push    0ffffff80h
  3689.         mov     edi, esp
  3690.         call    put_4bit_image
  3691.         pop     eax
  3692.         pop     edi
  3693.         ret
  3694. ;--------------------------------------
  3695. align 4
  3696. @@:
  3697.         push    ebp esi ebp
  3698.         cmp     esi, 8
  3699.         jnz     @f
  3700.         mov     ebp, putimage_get8bpp
  3701.         mov     esi, putimage_init8bpp
  3702.         jmp     sys_putimage_bpp
  3703. ;--------------------------------------
  3704. align 4
  3705. @@:
  3706.         cmp     esi, 9
  3707.         jnz     @f
  3708.         mov     ebp, putimage_get9bpp
  3709.         mov     esi, putimage_init9bpp
  3710.         jmp     sys_putimage_bpp
  3711. ;--------------------------------------
  3712. align 4
  3713. @@:
  3714.         cmp     esi, 15
  3715.         jnz     @f
  3716.         mov     ebp, putimage_get15bpp
  3717.         mov     esi, putimage_init15bpp
  3718.         jmp     sys_putimage_bpp
  3719. ;--------------------------------------
  3720. align 4
  3721. @@:
  3722.         cmp     esi, 16
  3723.         jnz     @f
  3724.         mov     ebp, putimage_get16bpp
  3725.         mov     esi, putimage_init16bpp
  3726.         jmp     sys_putimage_bpp
  3727. ;--------------------------------------
  3728. align 4
  3729. @@:
  3730.         cmp     esi, 24
  3731.         jnz     @f
  3732.         mov     ebp, putimage_get24bpp
  3733.         mov     esi, putimage_init24bpp
  3734.         jmp     sys_putimage_bpp
  3735. ;--------------------------------------
  3736. align 4
  3737. @@:
  3738.         cmp     esi, 32
  3739.         jnz     @f
  3740.         mov     ebp, putimage_get32bpp
  3741.         mov     esi, putimage_init32bpp
  3742.         jmp     sys_putimage_bpp
  3743. ;--------------------------------------
  3744. align 4
  3745. @@:
  3746.         pop     ebp esi ebp
  3747.         ret
  3748. ;-----------------------------------------------------------------------------
  3749. align 4
  3750. put_mono_image:
  3751.         push    ebp esi ebp
  3752.         mov     ebp, putimage_get1bpp
  3753.         mov     esi, putimage_init1bpp
  3754.         jmp     sys_putimage_bpp
  3755. ;-----------------------------------------------------------------------------
  3756. align 4
  3757. put_2bit_image:
  3758.         push    ebp esi ebp
  3759.         mov     ebp, putimage_get2bpp
  3760.         mov     esi, putimage_init2bpp
  3761.         jmp     sys_putimage_bpp
  3762. ;-----------------------------------------------------------------------------
  3763. align 4
  3764. put_4bit_image:
  3765.         push    ebp esi ebp
  3766.         mov     ebp, putimage_get4bpp
  3767.         mov     esi, putimage_init4bpp
  3768.         jmp     sys_putimage_bpp
  3769. ;-----------------------------------------------------------------------------
  3770. align 4
  3771. putimage_init24bpp:
  3772.         lea     eax, [eax*3]
  3773. putimage_init8bpp:
  3774. putimage_init9bpp:
  3775.         ret
  3776. ;-----------------------------------------------------------------------------
  3777. align 16
  3778. putimage_get24bpp:
  3779.         movzx   eax, byte [esi+2]
  3780.         shl     eax, 16
  3781.         mov     ax, [esi]
  3782.         add     esi, 3
  3783.         ret     4
  3784. ;-----------------------------------------------------------------------------
  3785. align 16
  3786. putimage_get8bpp:
  3787.         movzx   eax, byte [esi]
  3788.         push    edx
  3789.         mov     edx, [esp+8]
  3790.         mov     eax, [edx + eax*4]
  3791.         pop     edx
  3792.         inc     esi
  3793.         ret     4
  3794. ;-----------------------------------------------------------------------------
  3795. align 16
  3796. putimage_get9bpp:
  3797.         lodsb
  3798.         mov     ah, al
  3799.         shl     eax, 8
  3800.         mov     al, ah
  3801.         ret     4
  3802. ;-----------------------------------------------------------------------------
  3803. align 4
  3804. putimage_init1bpp:
  3805.         add     eax, ecx
  3806.         push    ecx
  3807.         add     eax, 7
  3808.         add     ecx, 7
  3809.         shr     eax, 3
  3810.         shr     ecx, 3
  3811.         sub     eax, ecx
  3812.         pop     ecx
  3813.         ret
  3814. ;-----------------------------------------------------------------------------
  3815. align 16
  3816. putimage_get1bpp:
  3817.         push    edx
  3818.         mov     edx, [esp+8]
  3819.         mov     al, [edx]
  3820.         add     al, al
  3821.         jnz     @f
  3822.         lodsb
  3823.         adc     al, al
  3824. @@:
  3825.         mov     [edx], al
  3826.         sbb     eax, eax
  3827.         and     eax, [edx+8]
  3828.         add     eax, [edx+4]
  3829.         pop     edx
  3830.         ret     4
  3831. ;-----------------------------------------------------------------------------
  3832. align 4
  3833. putimage_init2bpp:
  3834.         add     eax, ecx
  3835.         push    ecx
  3836.         add     ecx, 3
  3837.         add     eax, 3
  3838.         shr     ecx, 2
  3839.         shr     eax, 2
  3840.         sub     eax, ecx
  3841.         pop     ecx
  3842.         ret
  3843. ;-----------------------------------------------------------------------------
  3844. align 16
  3845. putimage_get2bpp:
  3846.         push    edx
  3847.         mov     edx, [esp+8]
  3848.         mov     al, [edx]
  3849.         mov     ah, al
  3850.         shr     al, 6
  3851.         shl     ah, 2
  3852.         jnz     .nonewbyte
  3853.         lodsb
  3854.         mov     ah, al
  3855.         shr     al, 6
  3856.         shl     ah, 2
  3857.         add     ah, 1
  3858. .nonewbyte:
  3859.         mov     [edx], ah
  3860.         mov     edx, [edx+4]
  3861.         movzx   eax, al
  3862.         mov     eax, [edx + eax*4]
  3863.         pop     edx
  3864.         ret     4
  3865. ;-----------------------------------------------------------------------------
  3866. align 4
  3867. putimage_init4bpp:
  3868.         add     eax, ecx
  3869.         push    ecx
  3870.         add     ecx, 1
  3871.         inc     eax      ;add   eax, 1
  3872.         shr     ecx, 1
  3873.         shr     eax, 1
  3874.         sub     eax, ecx
  3875.         pop     ecx
  3876.         ret
  3877. ;-----------------------------------------------------------------------------
  3878. align 16
  3879. putimage_get4bpp:
  3880.         push    edx
  3881.         mov     edx, [esp+8]
  3882.         add     byte [edx], 80h
  3883.         jc      @f
  3884.         movzx   eax, byte [edx+1]
  3885.         mov     edx, [edx+4]
  3886.         and     eax, 0x0F
  3887.         mov     eax, [edx + eax*4]
  3888.         pop     edx
  3889.         ret     4
  3890. @@:
  3891.         movzx   eax, byte [esi]
  3892.         add     esi, 1
  3893.         mov     [edx+1], al
  3894.         shr     eax, 4
  3895.         mov     edx, [edx+4]
  3896.         mov     eax, [edx + eax*4]
  3897.         pop     edx
  3898.         ret     4
  3899. ;-----------------------------------------------------------------------------
  3900. align 4
  3901. putimage_init32bpp:
  3902.         shl     eax, 2
  3903.         ret
  3904. ;-----------------------------------------------------------------------------
  3905. align 16
  3906. putimage_get32bpp:
  3907.         lodsd
  3908.         ret     4
  3909. ;-----------------------------------------------------------------------------
  3910. align 4
  3911. putimage_init15bpp:
  3912. putimage_init16bpp:
  3913.         add     eax, eax
  3914.         ret
  3915. ;-----------------------------------------------------------------------------
  3916. align 16
  3917. putimage_get15bpp:
  3918. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  3919.         push    ecx edx
  3920.         movzx   eax, word [esi]
  3921.         add     esi, 2
  3922.         mov     ecx, eax
  3923.         mov     edx, eax
  3924.         and     eax, 0x1F
  3925.         and     ecx, 0x1F shl 5
  3926.         and     edx, 0x1F shl 10
  3927.         shl     eax, 3
  3928.         shl     ecx, 6
  3929.         shl     edx, 9
  3930.         or      eax, ecx
  3931.         or      eax, edx
  3932.         pop     edx ecx
  3933.         ret     4
  3934. ;-----------------------------------------------------------------------------
  3935. align 16
  3936. putimage_get16bpp:
  3937. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  3938.         push    ecx edx
  3939.         movzx   eax, word [esi]
  3940.         add     esi, 2
  3941.         mov     ecx, eax
  3942.         mov     edx, eax
  3943.         and     eax, 0x1F
  3944.         and     ecx, 0x3F shl 5
  3945.         and     edx, 0x1F shl 11
  3946.         shl     eax, 3
  3947.         shl     ecx, 5
  3948.         shl     edx, 8
  3949.         or      eax, ecx
  3950.         or      eax, edx
  3951.         pop     edx ecx
  3952.         ret     4
  3953. ;-----------------------------------------------------------------------------
  3954. ;align 4
  3955. ; eax x beginning
  3956. ; ebx y beginning
  3957. ; ecx x end
  3958.         ; edx y end
  3959. ; edi color
  3960. ;__sys_drawbar:
  3961. ;        mov     esi, [current_slot_idx]
  3962. ;        shl     esi, BSF sizeof.WDATA
  3963. ;        add     eax, [window_data+esi+WDATA.clientbox.left]
  3964. ;        add     ecx, [window_data+esi+WDATA.clientbox.left]
  3965. ;        add     ebx, [window_data+esi+WDATA.clientbox.top]
  3966. ;        add     edx, [window_data+esi+WDATA.clientbox.top]
  3967. ;--------------------------------------
  3968. ;align 4
  3969. ;.forced:
  3970. ;        call    vesa20_drawbar
  3971. ;        call    [draw_pointer]
  3972. ;        ret
  3973. ;-----------------------------------------------------------------------------
  3974. if used _rdtsc
  3975. _rdtsc:
  3976.         bt      [cpu_caps], CAPS_TSC
  3977.         jnc     .ret_rdtsc
  3978.         rdtsc
  3979.         ret
  3980.    .ret_rdtsc:
  3981.         mov     edx, 0xffffffff
  3982.         mov     eax, 0xffffffff
  3983.         ret
  3984. end if
  3985.  
  3986. sys_msg_board_str:
  3987.  
  3988.         pushad
  3989.    @@:
  3990.         cmp     [esi], byte 0
  3991.         je      @f
  3992.         mov     ebx, 1
  3993.         movzx   ecx, byte [esi]
  3994.         call    sys_msg_board
  3995.         inc     esi
  3996.         jmp     @b
  3997.    @@:
  3998.         popad
  3999.         ret
  4000.  
  4001. sys_msg_board_byte:
  4002. ; in: al = byte to display
  4003. ; out: nothing
  4004. ; destroys: nothing
  4005.         pushad
  4006.         mov     ecx, 2
  4007.         shl     eax, 24
  4008.         jmp     @f
  4009.  
  4010. sys_msg_board_word:
  4011. ; in: ax = word to display
  4012. ; out: nothing
  4013. ; destroys: nothing
  4014.         pushad
  4015.         mov     ecx, 4
  4016.         shl     eax, 16
  4017.         jmp     @f
  4018.  
  4019. sys_msg_board_dword:
  4020. ; in: eax = dword to display
  4021. ; out: nothing
  4022. ; destroys: nothing
  4023.         pushad
  4024.         mov     ecx, 8
  4025. @@:
  4026.         push    ecx
  4027.         rol     eax, 4
  4028.         push    eax
  4029.         and     al, 0xF
  4030.         cmp     al, 10
  4031.         sbb     al, 69h
  4032.         das
  4033.         mov     cl, al
  4034.         xor     ebx, ebx
  4035.         inc     ebx
  4036.         call    sys_msg_board
  4037.         pop     eax
  4038.         pop     ecx
  4039.         loop    @b
  4040.         popad
  4041.         ret
  4042.  
  4043. msg_board_data_size = 65536 ; Must be power of two
  4044.  
  4045. uglobal
  4046. msg_board_data  rb  msg_board_data_size
  4047. msg_board_count dd  ?
  4048. endg
  4049.  
  4050. iglobal
  4051. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  4052. endg
  4053.  
  4054. sys_msg_board:
  4055. ; ebx=1 -> write, cl = byte to write
  4056. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  4057.         push    eax ebx
  4058.         mov     eax, ebx
  4059.         mov     ebx, ecx
  4060.         mov     ecx, [msg_board_count]
  4061.         cmp     eax, 1
  4062.         jne     .read
  4063.  
  4064. if defined debug_com_base
  4065.         push    dx ax
  4066. @@: ; wait for empty transmit register
  4067.         mov     dx, debug_com_base+5
  4068.         in      al, dx
  4069.         test    al, 1 shl 5
  4070.         jz      @r
  4071.         mov     dx, debug_com_base      ; Output the byte
  4072.         mov     al, bl
  4073.         out     dx, al
  4074.         pop     ax dx
  4075. end if
  4076.  
  4077.         mov     [msg_board_data+ecx], bl
  4078.         cmp     byte [debug_direct_print], 1
  4079.         jnz     .end
  4080.         pusha
  4081.         lea     edx, [msg_board_data+ecx]
  4082.         mov     ecx, 0x40FFFFFF
  4083.         mov     ebx, [msg_board_pos]
  4084.         mov     edi, 1
  4085.         mov     esi, edi ;1
  4086.         call    dtext
  4087.         popa
  4088.         add     word [msg_board_pos+2], 6
  4089.         cmp     word [msg_board_pos+2], 105*6
  4090.         jnc     @f
  4091.         cmp     bl, 10
  4092.         jnz     .end
  4093. @@:
  4094.         mov     word [msg_board_pos+2], 42*6
  4095.         add     word [msg_board_pos], 10
  4096.         mov     eax, [_display.height]
  4097.         sub     eax, 10
  4098.         cmp     ax, word [msg_board_pos]
  4099.         jnc     @f
  4100.         mov     word [msg_board_pos], 10
  4101. @@:
  4102.         pusha
  4103.         mov     eax, [msg_board_pos]
  4104.         movzx   ebx, ax
  4105.         shr     eax, 16
  4106.         mov     edx, 105*6
  4107.         xor     ecx, ecx
  4108.         mov     edi, 1
  4109.         mov     esi, 9
  4110. @@:
  4111.         call    hline
  4112.         inc     ebx
  4113.         dec     esi
  4114.         jnz     @b
  4115.         popa
  4116. .end:
  4117.         inc     ecx
  4118.         and     ecx, msg_board_data_size - 1
  4119.         mov     [msg_board_count], ecx
  4120. .ret:
  4121.         pop     ebx eax
  4122.         ret
  4123.  
  4124. .read:
  4125.         cmp     eax, 2
  4126.         jne     .ret
  4127.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4128.         test    ecx, ecx
  4129.         jnz     @f
  4130.         mov     [esp + SYSCALL_STACK.eax], ecx
  4131.         mov     [esp + SYSCALL_STACK.ebx], ecx
  4132.         ret
  4133. @@:
  4134.         mov     eax, msg_board_data+1
  4135.         mov     ebx, msg_board_data
  4136.         movzx   edx, byte [ebx]
  4137.         call    memmove
  4138.         dec     [msg_board_count]
  4139.         mov     [esp + SYSCALL_STACK.eax], edx
  4140.         mov     [esp + SYSCALL_STACK.ebx], 1
  4141.         ret
  4142.  
  4143. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4144. ;; 61 sys function.                                                ;;
  4145. ;; in eax=61,ebx in [1..3]                                         ;;
  4146. ;; out eax                                                         ;;
  4147. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4148. iglobal
  4149. align 4
  4150. f61call:
  4151.            dd sys_gs.1   ; resolution
  4152.            dd sys_gs.2   ; bits per pixel
  4153.            dd sys_gs.3   ; bytes per scanline
  4154. endg
  4155.  
  4156.  
  4157. align 4
  4158.  
  4159. sys_gs:                         ; direct screen access
  4160.         dec     ebx
  4161.         cmp     ebx, 2
  4162.         ja      .not_support
  4163.         jmp     dword [f61call + ebx*4]
  4164. .not_support:
  4165.         or      [esp + SYSCALL_STACK.eax], dword -1
  4166.         ret
  4167.  
  4168.  
  4169. .1:                             ; resolution
  4170.         mov     eax, [_display.width]
  4171.         shl     eax, 16
  4172.         mov     ax, word [_display.height]
  4173.         mov     [esp + SYSCALL_STACK.eax], eax
  4174.         ret
  4175. .2:                             ; bits per pixel
  4176.         mov     eax, [_display.bits_per_pixel]
  4177.         mov     [esp + SYSCALL_STACK.eax], eax
  4178.         ret
  4179. .3:                             ; bytes per scanline
  4180.         mov     eax, [_display.lfb_pitch]
  4181.         mov     [esp + SYSCALL_STACK.eax], eax
  4182.         ret
  4183.  
  4184. align 4
  4185. syscall_getscreensize:                  ; GetScreenSize
  4186.         mov     ax, word [_display.width]
  4187.         dec     ax
  4188.         shl     eax, 16
  4189.         mov     ax, word [_display.height]
  4190.         dec     ax
  4191.         mov     [esp + SYSCALL_STACK.eax], eax
  4192.         ret
  4193. ;-----------------------------------------------------------------------------
  4194. align 4
  4195. syscall_cdaudio:
  4196. ; ECX - position of CD/DVD-drive
  4197. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  4198. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  4199. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  4200.         cmp     ecx, 11
  4201.         ja      .exit
  4202.  
  4203.         mov     eax, ecx
  4204.         shr     eax, 2
  4205.         lea     eax, [eax*5]
  4206.         mov     al, [eax + DRIVE_DATA + 1]
  4207.  
  4208.         push    ecx ebx
  4209.         mov     ebx, ecx
  4210.         and     ebx, 11b
  4211.         shl     ebx, 1
  4212.         mov     cl, 6
  4213.         sub     cl, bl
  4214.         shr     al, cl
  4215.         test    al, 2 ; it's not an ATAPI device
  4216.         pop     ebx ecx
  4217.  
  4218.         jz      .exit
  4219.  
  4220.         cmp     ebx, 4
  4221.         je      .eject
  4222.  
  4223.         cmp     ebx, 5
  4224.         je      .load
  4225. ;--------------------------------------
  4226. .exit:
  4227.         ret
  4228. ;--------------------------------------
  4229. .load:
  4230.         call    .reserve
  4231.         call    LoadMedium
  4232.         jmp     .free
  4233. ;--------------------------------------
  4234. .eject:
  4235.         call    .reserve
  4236.         call    clear_CD_cache
  4237.         call    allow_medium_removal
  4238.         call    EjectMedium
  4239.         jmp     .free
  4240. ;--------------------------------------
  4241. .reserve:
  4242.         call    reserve_cd
  4243.  
  4244.         mov     ebx, ecx
  4245.         inc     ebx
  4246.         mov     [cdpos], ebx
  4247.  
  4248.         mov     eax, ecx
  4249.         shr     eax, 1
  4250.         and     eax, 1
  4251.         inc     eax
  4252.         mov     [ChannelNumber], al
  4253.         mov     eax, ecx
  4254.         and     eax, 1
  4255.         mov     [DiskNumber], al
  4256.         call    reserve_cd_channel
  4257.         ret
  4258. ;--------------------------------------
  4259. .free:
  4260.         call    free_cd_channel
  4261.         and     [cd_status], 0
  4262.         ret
  4263. ;-----------------------------------------------------------------------------
  4264. align 4
  4265. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  4266.         cmp     ebx, [_display.width]
  4267.         jb      @f
  4268.         cmp     ecx, [_display.height]
  4269.         jb      @f
  4270.         xor     eax, eax
  4271.         jmp     .store
  4272. ;--------------------------------------
  4273. align 4
  4274. @@:
  4275.         mov     eax, [d_width_calc_area + ecx*4]
  4276.         add     eax, [_display.win_map]
  4277.         movzx   eax, byte[eax+ebx]        ; get value for current point
  4278. ;--------------------------------------
  4279. align 4
  4280. .store:
  4281.         mov     [esp + SYSCALL_STACK.eax], eax
  4282.         ret
  4283. ;-----------------------------------------------------------------------------
  4284. align 4
  4285. syscall_getpixel:                       ; GetPixel
  4286.         mov     ecx, [_display.width]
  4287.         xor     edx, edx
  4288.         mov     eax, ebx
  4289.         div     ecx
  4290.         mov     ebx, edx
  4291.         xchg    eax, ebx
  4292.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4293.         call    dword [GETPIXEL]; eax - x, ebx - y
  4294.         mov     [esp + SYSCALL_STACK.eax], ecx
  4295.         ret
  4296. ;-----------------------------------------------------------------------------
  4297. align 4
  4298. syscall_getarea:
  4299. ;eax = 36
  4300. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4301. ;ecx = [size x]*65536 + [size y]
  4302. ;edx = [start x]*65536 + [start y]
  4303.         pushad
  4304.         mov     edi, ebx
  4305.         mov     eax, edx
  4306.         shr     eax, 16
  4307.         mov     ebx, edx
  4308.         and     ebx, 0xffff
  4309.         dec     eax
  4310.         dec     ebx
  4311.      ; eax - x, ebx - y
  4312.         mov     edx, ecx
  4313.  
  4314.         shr     ecx, 16
  4315.         and     edx, 0xffff
  4316.         mov     esi, ecx
  4317.      ; ecx - size x, edx - size y
  4318.  
  4319.         mov     ebp, edx
  4320.         lea     ebp, [ebp*3]
  4321.         imul    ebp, esi
  4322.         stdcall is_region_userspace, edi, ebp
  4323.         jnz     .exit
  4324.  
  4325.         mov     ebp, edx
  4326.         dec     ebp
  4327.         lea     ebp, [ebp*3]
  4328.  
  4329.         imul    ebp, esi
  4330.  
  4331.         mov     esi, ecx
  4332.         dec     esi
  4333.         lea     esi, [esi*3]
  4334.  
  4335.         add     ebp, esi
  4336.         add     ebp, edi
  4337.  
  4338.         add     ebx, edx
  4339. ;--------------------------------------
  4340. align 4
  4341. .start_y:
  4342.         push    ecx edx
  4343. ;--------------------------------------
  4344. align 4
  4345. .start_x:
  4346.         push    eax ebx ecx
  4347.         add     eax, ecx
  4348.  
  4349.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4350.         call    dword [GETPIXEL]; eax - x, ebx - y
  4351.  
  4352.         mov     [ebp], cx
  4353.         shr     ecx, 16
  4354.         mov     [ebp+2], cl
  4355.  
  4356.         pop     ecx ebx eax
  4357.         sub     ebp, 3
  4358.         dec     ecx
  4359.         jnz     .start_x
  4360.         pop     edx ecx
  4361.         dec     ebx
  4362.         dec     edx
  4363.         jnz     .start_y
  4364.        
  4365. .exit:
  4366.         popad
  4367.         ret
  4368. ;-----------------------------------------------------------------------------
  4369.  
  4370. align 4
  4371. syscall_threads:                        ; CreateThreads
  4372. ;
  4373. ;   ecx=thread entry point
  4374. ;   edx=thread stack pointer
  4375. ;
  4376. ; on return : eax = pid
  4377.         xor     ebx, ebx
  4378.         call    new_sys_threads
  4379.  
  4380.         mov     [esp + SYSCALL_STACK.eax], eax
  4381.         ret
  4382.  
  4383. ;------------------------------------------------------------------------------
  4384. align 4
  4385. calculate_fast_getting_offset_for_WinMapAddress:
  4386. ; calculate data area for fast getting offset to _WinMapAddress
  4387.         xor     eax, eax
  4388.         mov     ecx, [_display.height]
  4389.         mov     edi, d_width_calc_area
  4390.         cld
  4391. @@:
  4392.         stosd
  4393.         add     eax, [_display.width]
  4394.         dec     ecx
  4395.         jnz     @r
  4396.         ret
  4397. ;------------------------------------------------------------------------------
  4398. align 4
  4399. calculate_fast_getting_offset_for_LFB:
  4400. ; calculate data area for fast getting offset to LFB
  4401.         xor     eax, eax
  4402.         mov     ecx, [_display.height]
  4403.         mov     edi, BPSLine_calc_area
  4404.         cld
  4405. @@:
  4406.         stosd
  4407.         add     eax, [_display.lfb_pitch]
  4408.         dec     ecx
  4409.         jnz     @r
  4410.         ret
  4411. ;------------------------------------------------------------------------------
  4412. align 4
  4413. set_screen:
  4414. ; in:
  4415. ; eax - new Screen_Max_X
  4416. ; ecx - new BytesPerScanLine
  4417. ; edx - new Screen_Max_Y
  4418.  
  4419.         pushfd
  4420.         cli
  4421.  
  4422.         mov     [_display.lfb_pitch], ecx
  4423.  
  4424.         mov     [screen_workarea.right], eax
  4425.         mov     [screen_workarea.bottom], edx
  4426.  
  4427.         push    ebx
  4428.         push    esi
  4429.         push    edi
  4430.  
  4431.         pushad
  4432.  
  4433.         cmp     [do_not_touch_winmap], 1
  4434.         je      @f
  4435.  
  4436.         stdcall kernel_free, [_display.win_map]
  4437.  
  4438.         mov     eax, [_display.width]
  4439.         mul     [_display.height]
  4440.         mov     [_display.win_map_size], eax
  4441.  
  4442.         stdcall kernel_alloc, eax
  4443.         mov     [_display.win_map], eax
  4444.         test    eax, eax
  4445.         jz      .epic_fail
  4446. ; store for f.18.24
  4447.         mov     eax, [_display.width]
  4448.         mov     [display_width_standard], eax
  4449.  
  4450.         mov     eax, [_display.height]
  4451.         mov     [display_height_standard], eax
  4452. @@:
  4453.         call    calculate_fast_getting_offset_for_WinMapAddress
  4454. ; for Qemu or non standart video cards
  4455. ; Unfortunately [BytesPerScanLine] does not always
  4456. ;                             equal to [_display.width] * [ScreenBPP] / 8
  4457.         call    calculate_fast_getting_offset_for_LFB
  4458.         popad
  4459.  
  4460.         call    repos_windows
  4461.         xor     eax, eax
  4462.         xor     ebx, ebx
  4463.         mov     ecx, [_display.width]
  4464.         mov     edx, [_display.height]
  4465.         dec     ecx
  4466.         dec     edx
  4467.         call    calculatescreen
  4468.         pop     edi
  4469.         pop     esi
  4470.         pop     ebx
  4471.  
  4472.         popfd
  4473.         ret
  4474.  
  4475. .epic_fail:
  4476.         hlt                     ; Houston, we've had a problem
  4477.  
  4478. ; --------------- APM ---------------------
  4479. uglobal
  4480. apm_entry       dp      0
  4481. apm_vf          dd      0
  4482. endg
  4483.  
  4484. align 4
  4485. sys_apm:
  4486.         xor     eax, eax
  4487.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4488.         jne     @f
  4489.         inc     eax
  4490.         or      dword [esp + 44], eax   ; error
  4491.         add     eax, 7
  4492.         mov     dword [esp + SYSCALL_STACK.eax], eax   ; 32-bit protected-mode interface not supported
  4493.         ret
  4494.  
  4495. @@:
  4496. ;       xchg    eax, ecx
  4497. ;       xchg    ebx, ecx
  4498.  
  4499.         cmp     dx, 3
  4500.         ja      @f
  4501.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4502.         mov     eax, [apm_vf]
  4503.         mov     [esp + SYSCALL_STACK.eax], eax
  4504.         shr     eax, 16
  4505.         mov     [esp + SYSCALL_STACK.ecx], eax
  4506.         ret
  4507.  
  4508. @@:
  4509.  
  4510.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4511.         xchg    [master_tab], esi
  4512.         push    esi
  4513.         mov     edi, cr3
  4514.         mov     cr3, edi                ;flush TLB
  4515.  
  4516.         call    pword [apm_entry]       ;call APM BIOS
  4517.  
  4518.         xchg    eax, [esp]
  4519.         mov     [master_tab], eax
  4520.         mov     eax, cr3
  4521.         mov     cr3, eax
  4522.         pop     eax
  4523.  
  4524.         mov     [esp + SYSCALL_STACK.edi], edi
  4525.         mov     [esp + SYSCALL_STACK.esi], esi
  4526.         mov     [esp + SYSCALL_STACK.ebx], ebx
  4527.         mov     [esp + SYSCALL_STACK.edx], edx
  4528.         mov     [esp + SYSCALL_STACK.ecx], ecx
  4529.         mov     [esp + SYSCALL_STACK.eax], eax
  4530.         setc    al
  4531.         and     [esp + 44], byte 0xfe
  4532.         or      [esp + 44], al
  4533.         ret
  4534. ; -----------------------------------------
  4535.  
  4536. align 4
  4537. undefined_syscall:                      ; Undefined system call
  4538.         mov     [esp + SYSCALL_STACK.eax], dword -1
  4539.         ret
  4540.  
  4541. align 4
  4542. ; @brief Check if given memory region lays in lower 2gb (userspace memory) or not
  4543. ; @param base Base address of region
  4544. ; @param len Lenght of region
  4545. ; @return ZF = 1 if region in userspace memory,
  4546. ;         ZF = 0 otherwise
  4547. proc is_region_userspace stdcall, base:dword, len:dword
  4548.         push    eax
  4549.         mov     eax, [base]
  4550.  
  4551.         cmp     eax, OS_BASE-1
  4552.         ja      @f              ; zf
  4553.  
  4554.         add     eax, [len]
  4555.         jc      @f              ; zf
  4556.         cmp     eax, OS_BASE
  4557.         ja      @f              ; zf
  4558.  
  4559.         cmp     eax, eax        ; ZF
  4560. @@:
  4561.         pop     eax
  4562.         ret
  4563. endp
  4564.  
  4565. align 4
  4566. ; @brief Check whether given string lays in userspace memory, i.e. below OS_BASE
  4567. ; @param base Base address of string
  4568. ; @return ZF = 1 if string in userspace memory,
  4569. ;         zf = 0 otherwise
  4570. proc is_string_userspace stdcall, base:dword
  4571.         push    eax ecx edi
  4572.         xor     eax, eax
  4573.         mov     edi, [base]
  4574.  
  4575.         mov     ecx, OS_BASE-1
  4576.         sub     ecx, edi
  4577.         jb      .done           ; zf
  4578.         inc     ecx
  4579.         cmp     ecx, 0x10000    ; don't allow strings larger than 64k?
  4580.         jbe     @f
  4581.         mov     ecx, 0x10000
  4582. @@:
  4583.         repnz scasb
  4584. .done:
  4585.         pop     edi ecx eax
  4586.         ret
  4587. endp
  4588.  
  4589. if ~ lang eq sp
  4590. diff16 "end of .text segment",0,$
  4591. end if
  4592.  
  4593. include "data32.inc"
  4594.  
  4595. __REV__ = __REV
  4596.  
  4597. if ~ lang eq sp
  4598. diff16 "end of kernel code",0,$
  4599. end if
  4600.