Subversion Repositories Kolibri OS

Rev

Rev 9910 | Rev 9917 | 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: 9911 $
  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, 4096
  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.         call    init_display
  901.         mov     eax, [def_cursor]
  902.         mov     [SLOT_BASE + APPDATA.cursor + sizeof.APPDATA], eax
  903.         mov     [SLOT_BASE + APPDATA.cursor + sizeof.APPDATA*2], eax
  904.  
  905. ; PRINT CPU FREQUENCY
  906.  
  907.         mov     esi, boot_cpufreq
  908.         call    boot_log
  909.  
  910.         cli
  911.         mov     ebx, [hpet_base]
  912.         test    ebx, ebx
  913.         jz      @F
  914.         mov     ebx, [ebx + HPET_COUNTER]
  915.  
  916.         rdtsc
  917.         mov     ecx, 1000
  918.         sub     eax, [hpet_tsc_start]
  919.         sbb     edx, [hpet_tsc_start + 4]
  920.         shld    edx, eax, 10
  921.         shl     eax, 10
  922.         mov     esi, eax
  923.         mov     eax, edx
  924.         mul     ecx
  925.         xchg    eax, esi
  926.         mul     ecx
  927.         adc     edx, esi
  928.         div     ebx
  929.         mul     ecx
  930.         div     [hpet_period]
  931.         mul     ecx
  932.         DEBUGF  1, "K : cpu frequency %u Hz\n", eax
  933.         jmp     .next
  934. @@:
  935.         rdtsc
  936.         mov     ecx, eax
  937.         mov     esi, 250            ; wait 1/4 a second
  938.         call    delay_ms
  939.         rdtsc
  940.  
  941.         sub     eax, ecx
  942.         xor     edx, edx
  943.         shld    edx, eax, 2
  944.         shl     eax, 2
  945. .next:
  946.         mov     dword [cpu_freq], eax
  947.         mov     dword [cpu_freq + 4], edx
  948.         mov     ebx, 1000000
  949.         div     ebx
  950.         mov     ebx, eax
  951.  
  952.         movzx   ecx, word [boot_y]
  953.         if lang eq ru
  954.         add     ecx, (10+19*6) shl 16 - 10
  955.         else if lang eq sp
  956.         add     ecx, (10+25*6) shl 16 - 10
  957.         else
  958.         add     ecx, (10+17*6) shl 16 - 10
  959.         end if
  960.  
  961.         mov     edx, 0xFFFFFF
  962.         xor     edi, edi
  963.         mov     eax, 0x00040000
  964.         inc     edi
  965.         call    display_number_force
  966.  
  967. ; SET VARIABLES
  968.  
  969.         call    set_variables
  970.  
  971. ; STACK AND FDC
  972.  
  973.         call    stack_init
  974.         call    fdc_init
  975.  
  976. ; PALETTE FOR 320x200 and 640x480 16 col
  977.  
  978.         cmp     [SCR_MODE], word 0x12
  979.         jne     no_pal_vga
  980.         mov     esi, boot_pal_vga
  981.         call    boot_log
  982.         call    paletteVGA
  983.       no_pal_vga:
  984.  
  985.         cmp     [SCR_MODE], word 0x13
  986.         jne     no_pal_ega
  987.         mov     esi, boot_pal_ega
  988.         call    boot_log
  989.         call    palette320x200
  990.       no_pal_ega:
  991.  
  992. ; LOAD DEFAULT SKIN
  993.  
  994.         call    load_default_skin
  995.  
  996. ; Protect I/O permission map
  997.  
  998.         mov     esi, [default_io_map]
  999.         stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
  1000.         add     esi, 0x1000
  1001.         stdcall map_page, esi, [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
  1002.  
  1003.         stdcall map_page, tss._io_map_0, \
  1004.                 [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map], PG_READ
  1005.         stdcall map_page, tss._io_map_1, \
  1006.                 [SLOT_BASE + sizeof.APPDATA + APPDATA.io_map + 4], PG_READ
  1007.  
  1008. ; SET KEYBOARD PARAMETERS
  1009.         mov     al, 0xf6       ; reset keyboard, scan enabled
  1010.         call    kb_write_wait_ack
  1011.         test    ah, ah
  1012.         jnz     .no_keyboard
  1013.  
  1014. iglobal
  1015. align 4
  1016. ps2_keyboard_functions:
  1017.         dd      .end - $
  1018.         dd      0       ; no close
  1019.         dd      ps2_set_lights
  1020. .end:
  1021. endg
  1022.         stdcall register_keyboard, ps2_keyboard_functions, 0
  1023.        ; mov   al, 0xED       ; Keyboard LEDs - only for testing!
  1024.        ; call  kb_write_wait_ack
  1025.        ; mov   al, 111b
  1026.        ; call  kb_write_wait_ack
  1027.  
  1028.         mov     al, 0xF3     ; set repeat rate & delay
  1029.         call    kb_write_wait_ack
  1030.         mov     al, 0; 30 250 ;00100010b ; 24 500  ;00100100b  ; 20 500
  1031.         call    kb_write_wait_ack
  1032.      ;// mike.dld [
  1033.         call    set_lights
  1034.      ;// mike.dld ]
  1035.         stdcall attach_int_handler, 1, irq1, 0
  1036.         DEBUGF  1, "K : IRQ1 return code %x\n", eax
  1037. .no_keyboard:
  1038.  
  1039. ; Load PS/2 mouse driver
  1040.         mov     esi, boot_setmouse
  1041.         call    boot_log
  1042.         stdcall load_pe_driver, szPS2MDriver, 0
  1043.  
  1044. ; LOAD FIRST APPLICATION
  1045.         cmp     byte [launcher_start], 1        ; Check if starting LAUNCHER is selected on blue screen (1 = yes)
  1046.         jnz     first_app_found
  1047.  
  1048.         cli
  1049.         mov     ebp, firstapp
  1050.         call    fs_execute_from_sysdir
  1051.         test    eax, eax
  1052.         jns     first_app_found
  1053.  
  1054.         mov     esi, boot_failed
  1055.         call    boot_log
  1056.  
  1057.         mov     eax, 0xDEADBEEF      ; otherwise halt
  1058.         hlt
  1059.  
  1060. first_app_found:
  1061.  
  1062. ; START MULTITASKING
  1063. preboot_blogesc = 0       ; start immediately after bootlog
  1064.  
  1065. ; A 'All set - press ESC to start' messages if need
  1066. if preboot_blogesc
  1067.         mov     esi, boot_tasking
  1068.         call    boot_log
  1069. .bll1:
  1070.         in      al, 0x60        ; wait for ESC key press
  1071.         cmp     al, 129
  1072.         jne     .bll1
  1073. end if
  1074.  
  1075.         mov     [timer_ticks_enable], 1         ; for cd driver
  1076.         sti
  1077.  
  1078.         call    mtrr_validate
  1079.  
  1080.         jmp     osloop
  1081.         ; Fly :)
  1082.  
  1083. uglobal
  1084. align 4
  1085. ap_initialized  dd      0
  1086. endg
  1087.  
  1088. ap_init_high:
  1089.         mov     ax, os_stack
  1090.         mov     bx, app_data
  1091.         mov     cx, app_tls
  1092.         mov     ss, ax
  1093.         mov     ds, bx
  1094.         mov     es, bx
  1095.         mov     fs, cx
  1096.         mov     gs, bx
  1097.         xor     esp, esp
  1098.         mov     eax, sys_proc - OS_BASE + PROC.pdt_0
  1099.         mov     cr3, eax
  1100.         lock inc [ap_initialized]
  1101.         jmp     idle_loop
  1102.  
  1103.  
  1104. include 'unpacker.inc'
  1105.  
  1106. align 4
  1107. boot_log:
  1108.         pushad
  1109.  
  1110.         mov     ebx, 10*65536
  1111.         mov     bx, word [boot_y]
  1112.         add     [boot_y], dword 10
  1113.         mov     ecx, 0x80ffffff; ASCIIZ string with white color
  1114.         xor     edi, edi
  1115.         mov     edx, esi
  1116.         inc     edi
  1117.         call    dtext
  1118.  
  1119.         mov     [novesachecksum], 1000
  1120.         call    checkVga_N13
  1121.  
  1122.         popad
  1123.  
  1124.         ret
  1125.  
  1126. ;-----------------------------------------------------------------------------
  1127. ; Register ramdisk file system
  1128. register_ramdisk:
  1129.         mov     esi, boot_initramdisk
  1130.         call    boot_log
  1131.         call    ramdisk_init
  1132.         ret
  1133.  
  1134. ; in: edx -> APPDATA for OS/IDLE slot
  1135. ; in: ebx = stack base
  1136. proc setup_os_slot
  1137.         xor     eax, eax
  1138.         mov     ecx, sizeof.APPDATA/4
  1139.         mov     edi, edx
  1140.         rep stosd
  1141.  
  1142.         mov     eax, tss+0x80
  1143.         call    get_pg_addr
  1144.         inc     eax
  1145.         mov     [edx + APPDATA.io_map], eax
  1146.         mov     eax, tss+0x1080
  1147.         call    get_pg_addr
  1148.         inc     eax
  1149.         mov     [edx + APPDATA.io_map + 4], eax
  1150.  
  1151.         mov     dword [edx + APPDATA.pl0_stack], ebx
  1152.         lea     edi, [ebx + RING0_STACK_SIZE]
  1153.         mov     dword [edx + APPDATA.fpu_state], edi
  1154.         mov     dword [edx + APPDATA.saved_esp0], edi
  1155.         mov     dword [edx + APPDATA.saved_esp], edi
  1156.         mov     dword [edx + APPDATA.terminate_protection], 1 ; make unkillable
  1157.  
  1158.         mov     esi, fpu_data
  1159.         mov     ecx, [xsave_area_size]
  1160.         add     ecx, 3
  1161.         shr     ecx, 2
  1162.         rep movsd
  1163.  
  1164.         lea     eax, [edx + APP_EV_OFFSET]
  1165.         mov     dword [edx + APPDATA.fd_ev], eax
  1166.         mov     dword [edx + APPDATA.bk_ev], eax
  1167.  
  1168.         lea     eax, [edx + APP_OBJ_OFFSET]
  1169.         mov     dword [edx + APPDATA.fd_obj], eax
  1170.         mov     dword [edx + APPDATA.bk_obj], eax
  1171.  
  1172.         mov     dword [edx + APPDATA.cur_dir], sysdir_path-2
  1173.  
  1174.         mov     [edx + APPDATA.process], sys_proc
  1175.  
  1176.         lea     ebx, [edx + APPDATA.list]
  1177.         lea     ecx, [sys_proc + PROC.thr_list]
  1178.         list_add_tail ebx, ecx
  1179.  
  1180.         mov     [edx + APPDATA.wnd_number], dh
  1181.         mov     byte [edx + APPDATA.tid], dh
  1182.  
  1183.         ret
  1184. endp
  1185.  
  1186. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1187. ;                                                                    ;
  1188. ;                    MAIN OS LOOP START                              ;
  1189. ;                                                                    ;
  1190. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1191. align 32
  1192. osloop:
  1193.         mov     edx, osloop_has_work?
  1194.         xor     ecx, ecx
  1195.         call    Wait_events
  1196.         xor     eax, eax
  1197.         xchg    eax, [osloop_nonperiodic_work]
  1198.         test    eax, eax
  1199.         jz      .no_periodic
  1200.  
  1201.         call    __sys_draw_pointer
  1202.         call    window_check_events
  1203.         call    mouse_check_events
  1204.         call    checkmisc
  1205.         call    checkVga_N13
  1206. ;--------------------------------------
  1207. .no_periodic:
  1208.         call    stack_handler
  1209.         call    check_fdd_motor_status
  1210.         call    check_ATAPI_device_event
  1211.         call    check_lights_state
  1212.         call    check_timers
  1213.  
  1214.         jmp     osloop
  1215. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1216. ;                                                                    ;
  1217. ;                      MAIN OS LOOP END                              ;
  1218. ;                                                                    ;
  1219. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1220. proc osloop_has_work?
  1221.         cmp     [osloop_nonperiodic_work], 0
  1222.         jnz     .yes
  1223.         call    stack_handler_has_work?
  1224.         jnz     .yes
  1225.         call    check_fdd_motor_status_has_work?
  1226.         jnz     .yes
  1227.         call    check_ATAPI_device_event_has_work?
  1228.         jnz     .yes
  1229.         call    check_lights_state_has_work?
  1230.         jnz     .yes
  1231.         call    check_timers_has_work?
  1232.         jnz     .yes
  1233. .no:
  1234.         xor     eax, eax
  1235.         ret
  1236. .yes:
  1237.         xor     eax, eax
  1238.         inc     eax
  1239.         ret
  1240. endp
  1241.  
  1242. proc wakeup_osloop
  1243.         mov     [osloop_nonperiodic_work], 1
  1244.         ret
  1245. endp
  1246.  
  1247. uglobal
  1248. align 4
  1249. osloop_nonperiodic_work dd      ?
  1250. endg
  1251.  
  1252. uglobal
  1253. align 64
  1254. idle_addr       rb      64
  1255. endg
  1256.  
  1257. idle_thread:
  1258.         sti
  1259.  
  1260. ; The following code can be executed by all CPUs in the system.
  1261. ; All other parts of the kernel do not expect multi-CPU.
  1262. ; Also, APs don't even have a stack here.
  1263. ; Beware. Don't do anything here. Anything at all.
  1264. idle_loop:
  1265.         cmp     [use_mwait_for_idle], 0
  1266.         jnz     .mwait
  1267.  
  1268. .hlt:
  1269.         hlt
  1270.         jmp     .hlt
  1271.  
  1272. .mwait:
  1273.         mov     eax, idle_addr
  1274.         xor     ecx, ecx
  1275.         xor     edx, edx
  1276.         monitor
  1277.         xor     ecx, ecx
  1278.         mov     eax, 20h        ; or 10h
  1279.         mwait
  1280.         jmp     .mwait
  1281.  
  1282.  
  1283. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1284. ;                                                                      ;
  1285. ;                   INCLUDED SYSTEM FILES                              ;
  1286. ;                                                                      ;
  1287. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1288.  
  1289.  
  1290. include "kernel32.inc"
  1291.  
  1292.  
  1293. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1294. ;                                                                      ;
  1295. ;                       KERNEL FUNCTIONS                               ;
  1296. ;                                                                      ;
  1297. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1298.  
  1299. reserve_irqs_ports:
  1300.  
  1301.  
  1302. ; RESERVE PORTS
  1303.         mov     eax, RESERVED_PORTS
  1304.         mov     ecx, 1
  1305.  
  1306.         mov     [eax], dword 4
  1307.  
  1308.         mov     [eax+16], ecx
  1309.         mov     [eax+16+4], dword 0
  1310.         mov     [eax+16+8], dword 0x2D
  1311.  
  1312.         mov     [eax+32], ecx
  1313.         mov     [eax+32+4], dword 0x30
  1314.         mov     [eax+32+8], dword 0x4D
  1315.  
  1316.         mov     [eax+48], ecx
  1317.         mov     [eax+48+4], dword 0x50
  1318.         mov     [eax+48+8], dword 0xDF
  1319.  
  1320.         mov     [eax+64], ecx
  1321.         mov     [eax+64+4], dword 0xE5
  1322.         mov     [eax+64+8], dword 0xFF
  1323.  
  1324.         ret
  1325.  
  1326.  
  1327. iglobal
  1328.   process_number dd 0x2
  1329. endg
  1330.  
  1331. set_variables:
  1332.  
  1333.         mov     ecx, 0x16                    ; flush port 0x60
  1334. @@:
  1335.         in      al, 0x60
  1336.         loop    @b
  1337.         push    eax
  1338.  
  1339.         mov     ax, [BOOT.y_res]
  1340.         shr     ax, 1
  1341.         shl     eax, 16
  1342.         mov     ax, [BOOT.x_res]
  1343.         shr     ax, 1
  1344.         mov     [MOUSE_X], eax
  1345.         call    wakeup_osloop
  1346.  
  1347.         xor     eax, eax
  1348.         mov     [BTN_ADDR], dword BUTTON_INFO ; address of button list
  1349.  
  1350.         mov     byte [KEY_COUNT], al              ; keyboard buffer
  1351.         mov     byte [BTN_COUNT], al              ; button buffer
  1352. ;        mov   [MOUSE_X],dword 100*65536+100    ; mouse x/y
  1353.  
  1354.         pop     eax
  1355.         ret
  1356. ;-----------------------------------------------------------------------------
  1357.  
  1358. align 4
  1359. display_number:
  1360. ; add check pointers
  1361.         test    bl, bl
  1362.         jz      @f
  1363.         bt      ebx, 30  ; check 30 bit
  1364.         jb      @f
  1365.         stdcall is_region_userspace, ecx, 4
  1366.         jz      @f
  1367.         ret
  1368. @@:
  1369.         jz      @f
  1370.         stdcall is_region_userspace, ecx, 8
  1371.         jz      @f
  1372.         ret
  1373. @@:
  1374.         test    esi, 0x08000000
  1375.         jz      @f
  1376.         stdcall is_region_userspace, edi, 1
  1377.         jz      @f
  1378.         ret
  1379. @@:
  1380. ;It is not optimization
  1381.         mov     eax, ebx
  1382.         mov     ebx, ecx
  1383.         mov     ecx, edx
  1384.         mov     edx, esi
  1385.         mov     esi, edi
  1386. ; eax = print type, al=0 -> ebx is number
  1387. ;                   al=1 -> ebx is pointer
  1388. ;                   ah=0 -> display decimal
  1389. ;                   ah=1 -> display hexadecimal
  1390. ;                   ah=2 -> display binary
  1391. ;                   eax bits 16-21 = number of digits to display (0-32)
  1392. ;                   eax bits 22-31 = reserved
  1393. ;
  1394. ; ebx = number or pointer
  1395. ; ecx = x shl 16 + y
  1396. ; edx = color
  1397.         xor     edi, edi
  1398. display_number_force:
  1399.         push    eax
  1400.         and     eax, 0x3fffffff
  1401.         cmp     eax, 0xffff     ; length > 0 ?
  1402.         pop     eax
  1403.         jge     .cont_displ
  1404.         ret
  1405.    .cont_displ:
  1406.         push    eax
  1407.         and     eax, 0x3fffffff
  1408.         cmp     eax, 61*0x10000  ; length <= 60 ?
  1409.         pop     eax
  1410.         jb      .cont_displ2
  1411.         ret
  1412.    .cont_displ2:
  1413.  
  1414.         pushad
  1415.  
  1416.         cmp     al, 1            ; ecx is a pointer ?
  1417.         jne     @f
  1418.         mov     ebp, ebx
  1419.         add     ebp, 4
  1420.         mov     ebp, [ebp] ;[ebp + std_application_base_address]
  1421.         mov     ebx, [ebx] ;[ebx + std_application_base_address]
  1422. @@:
  1423.         sub     esp, 64
  1424.  
  1425.         test    ah, ah            ; DECIMAL
  1426.         jnz     .no_display_desnum
  1427.  
  1428.         shr     eax, 16
  1429.         and     eax, 0xC03f
  1430. ;     and   eax,0x3f
  1431.         push    eax
  1432.         and     eax, 0x3f
  1433.         mov     edi, esp
  1434.         add     edi, 4+64-1
  1435.         mov     ecx, eax
  1436.         mov     eax, ebx
  1437.         mov     ebx, 10
  1438. @@:
  1439.         xor     edx, edx
  1440.         call    division_64_bits
  1441.         div     ebx
  1442.         add     dl, 48
  1443.         mov     [edi], dl
  1444.         dec     edi
  1445.         loop    @b
  1446.  
  1447.         pop     eax
  1448.         call    normalize_number
  1449.         call    draw_num_text
  1450.         add     esp, 64
  1451.         popad
  1452.         ret
  1453. .no_display_desnum:
  1454.  
  1455.         cmp     ah, 0x01         ; HEXADECIMAL
  1456.         jne     .no_display_hexnum
  1457.  
  1458.         shr     eax, 16
  1459.         and     eax, 0xC03f
  1460. ;     and   eax,0x3f
  1461.         push    eax
  1462.         and     eax, 0x3f
  1463.         mov     edi, esp
  1464.         add     edi, 4+64-1
  1465.         mov     ecx, eax
  1466.         mov     eax, ebx
  1467.         mov     ebx, 16
  1468. @@:
  1469.         xor     edx, edx
  1470.         call    division_64_bits
  1471.         div     ebx
  1472.    ;hexletters = __fdo_hexdigits
  1473.         add     edx, __fdo_hexdigits ;hexletters
  1474.         mov     dl, [edx]
  1475.         mov     [edi], dl
  1476.         dec     edi
  1477.         loop    @b
  1478.  
  1479.         pop     eax
  1480.         call    normalize_number
  1481.         call    draw_num_text
  1482.         add     esp, 64
  1483.         popad
  1484.         ret
  1485. .no_display_hexnum:
  1486.  
  1487.         cmp     ah, 0x02         ; BINARY
  1488.         jne     .no_display_binnum
  1489.  
  1490.         shr     eax, 16
  1491.         and     eax, 0xC03f
  1492. ;     and   eax,0x3f
  1493.         push    eax
  1494.         and     eax, 0x3f
  1495.         mov     edi, esp
  1496.         add     edi, 4+64-1
  1497.         mov     ecx, eax
  1498.         mov     eax, ebx
  1499.         mov     ebx, 2
  1500. @@:
  1501.         xor     edx, edx
  1502.         call    division_64_bits
  1503.         div     ebx
  1504.         add     dl, 48
  1505.         mov     [edi], dl
  1506.         dec     edi
  1507.         loop    @b
  1508.  
  1509.         pop     eax
  1510.         call    normalize_number
  1511.         call    draw_num_text
  1512.         add     esp, 64
  1513.         popad
  1514.         ret
  1515. .no_display_binnum:
  1516.  
  1517.         add     esp, 64
  1518.         popad
  1519.         ret
  1520.  
  1521. normalize_number:
  1522.         test    ah, 0x80
  1523.         jz      .continue
  1524.         mov     ecx, 48
  1525.         and     eax, 0x3f
  1526. @@:
  1527.         inc     edi
  1528.         cmp     [edi], cl
  1529.         jne     .continue
  1530.         dec     eax
  1531.         cmp     eax, 1
  1532.         ja      @r
  1533.         mov     al, 1
  1534. .continue:
  1535.         and     eax, 0x3f
  1536.         ret
  1537.  
  1538. division_64_bits:
  1539.         test    [esp+1+4], byte 0x40
  1540.         jz      .continue
  1541.         push    eax
  1542.         mov     eax, ebp
  1543.         div     ebx
  1544.         mov     ebp, eax
  1545.         pop     eax
  1546. .continue:
  1547.         ret
  1548.  
  1549. draw_num_text:
  1550.         mov     esi, eax
  1551.         mov     edx, 64+4
  1552.         sub     edx, eax
  1553.         add     edx, esp
  1554.         mov     ebx, [esp+64+32-8+4]
  1555. ; add window start x & y
  1556.  
  1557.         mov     edi, [current_slot_idx]
  1558.         mov     ecx, edi
  1559.         shl     edi, BSF sizeof.APPDATA
  1560.         shl     ecx, BSF sizeof.WDATA
  1561.  
  1562.         mov     eax, [window_data + ecx + WDATA.box.left]
  1563.         add     eax, [SLOT_BASE + edi + APPDATA.wnd_clientbox.left]
  1564.         shl     eax, 16
  1565.         add     eax, [window_data + ecx + WDATA.box.top]
  1566.         add     eax, [SLOT_BASE + edi + APPDATA.wnd_clientbox.top]
  1567.         add     ebx, eax
  1568.         mov     ecx, [esp+64+32-12+4]
  1569.         mov     eax, [esp+64+8]         ; background color (if given)
  1570.         mov     edi, [esp+64+4]
  1571.         and     ecx, 5FFFFFFFh
  1572.         bt      ecx, 27
  1573.         jnc     @f
  1574.         mov     edi, eax
  1575. @@:
  1576.         jmp     dtext
  1577. ;-----------------------------------------------------------------------------
  1578. align 4
  1579. sys_setup:
  1580. ;  1 = roland mpu midi base , base io address
  1581. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1582. ;  3 = not used
  1583. ;  4 = not used
  1584. ;  5 = system language, 1eng 2fi 3ger 4rus
  1585. ;  6 = not used
  1586. ;  7 = not used
  1587. ;  8 = not used
  1588. ;  9 = not used
  1589. ; 10 = not used
  1590. ; 11 = enable lba read
  1591. ; 12 = enable pci access
  1592. ;-----------------------------------------------------------------------------
  1593.         and     [esp + SYSCALL_STACK.eax], dword 0
  1594. ; F.21.1 - set MPU MIDI base port
  1595.         dec     ebx
  1596.         jnz     @f
  1597.  
  1598.         cmp     ecx, 0x100
  1599.         jb      @f
  1600.  
  1601.         mov     esi, 65535
  1602.         cmp     esi, ecx
  1603.         jb      @f
  1604.  
  1605.         mov     [midi_base], cx
  1606.         mov     word [mididp], cx
  1607.         inc     cx
  1608.         mov     word [midisp], cx
  1609.         ret
  1610. ;--------------------------------------
  1611. @@:
  1612. ; F.21.2 - set keyboard layout
  1613.         dec     ebx
  1614.         jnz     @f
  1615.  
  1616.         mov     eax, edx
  1617. ; 1 = normal layout
  1618.         dec     ecx
  1619.         jnz     .shift
  1620.  
  1621.         mov     ebx, keymap
  1622.         mov     ecx, 128
  1623.         call    memmove
  1624.         ret
  1625. ;--------------------------------------
  1626. .shift:
  1627. ; 2 = layout at pressed Shift
  1628.         dec     ecx
  1629.         jnz     .alt
  1630.  
  1631.         mov     ebx, keymap_shift
  1632.         mov     ecx, 128
  1633.         call    memmove
  1634.         ret
  1635. ;--------------------------------------
  1636. .alt:
  1637. ; 3 = layout at pressed Alt
  1638.         dec     ecx
  1639.         jnz     .country
  1640.  
  1641.         mov     ebx, keymap_alt
  1642.         mov     ecx, 128
  1643.         call    memmove
  1644.         ret
  1645. ;--------------------------------------
  1646. .country:
  1647. ; country identifier
  1648.         sub     ecx, 6
  1649.         jnz     .error
  1650.  
  1651.         mov     word [keyboard], dx
  1652.         ret
  1653. ;--------------------------------------
  1654. @@:
  1655. ; F.21.5 - set system language
  1656.         sub     ebx, 3
  1657.         jnz     @f
  1658.  
  1659.         mov     [syslang], ecx
  1660.         ret
  1661. ;--------------------------------------
  1662. @@:
  1663. ; F.21.11 - enable/disable low-level access to HD
  1664.         and     ecx, 1
  1665.         sub     ebx, 6
  1666.         jnz     @f
  1667.  
  1668.         mov     [lba_read_enabled], ecx
  1669.         ret
  1670. ;--------------------------------------
  1671. @@:
  1672. ; F.21.12 - enable/disable low-level access to PCI
  1673.         dec     ebx
  1674.         jnz     .error
  1675.  
  1676.         mov     [pci_access_enabled], ecx
  1677.         ret
  1678. ;--------------------------------------
  1679. .error:
  1680.         or      [esp + SYSCALL_STACK.eax], dword -1
  1681.         ret
  1682. ;-----------------------------------------------------------------------------
  1683. align 4
  1684. sys_getsetup:
  1685. ;  1 = roland mpu midi base , base io address
  1686. ;  2 = keyboard   1, base kaybap 2, shift keymap, 9 country 1eng 2fi 3ger 4rus
  1687. ;  3 = not used
  1688. ;  4 = not used
  1689. ;  5 = system language, 1eng 2fi 3ger 4rus
  1690. ;  6 = not used
  1691. ;  7 = not used
  1692. ;  8 = not used
  1693. ;  9 = get hs timer tic
  1694. ; 10 = not used
  1695. ; 11 = get the state "lba read"
  1696. ; 12 = get the state "pci access"
  1697. ;-----------------------------------------------------------------------------
  1698. ; F.26.1 - get MPU MIDI base port
  1699.         dec     ebx
  1700.         jnz     @f
  1701.  
  1702.         movzx   eax, [midi_base]
  1703.         mov     [esp + SYSCALL_STACK.eax], eax
  1704.         ret
  1705. ;--------------------------------------
  1706. @@:
  1707. ; F.26.2 - get keyboard layout
  1708.         dec     ebx
  1709.         jnz     @f
  1710.  
  1711.         mov     ebx, edx
  1712.         ; if given memory address belongs to kernel then error
  1713.         stdcall is_region_userspace, ebx, 128
  1714.         jnz     .addr_error
  1715. ; 1 = normal layout
  1716.         dec     ecx
  1717.         jnz     .shift
  1718.  
  1719.         mov     eax, keymap
  1720.         mov     ecx, 128
  1721.         call    memmove
  1722.         ret
  1723. ;--------------------------------------
  1724. .shift:
  1725. ; 2 = layout with pressed Shift
  1726.         dec     ecx
  1727.         jnz     .alt
  1728.  
  1729.         mov     eax, keymap_shift
  1730.         mov     ecx, 128
  1731.         call    memmove
  1732.         ret
  1733. ;--------------------------------------
  1734. .alt:
  1735. ; 3 = layout with pressed Alt
  1736.         dec     ecx
  1737.         jne     .country
  1738.  
  1739.         mov     eax, keymap_alt
  1740.         mov     ecx, 128
  1741.         call    memmove
  1742.         ret
  1743. ;--------------------------------------
  1744. .country:
  1745. ; 9 = country identifier
  1746.         sub     ecx, 6
  1747.         jnz     .error
  1748.  
  1749.         movzx   eax, word [keyboard]
  1750.         mov     [esp + SYSCALL_STACK.eax], eax
  1751.         ret
  1752.  
  1753. .addr_error:    ; if given memory address is illegal
  1754.         or      dword [esp + SYSCALL_STACK.eax], -1
  1755.         ret        
  1756. ;--------------------------------------
  1757. @@:
  1758. ; F.26.5 - get system language
  1759.         sub     ebx, 3
  1760.         jnz     @f
  1761.  
  1762.         mov     eax, [syslang]
  1763.         mov     [esp + SYSCALL_STACK.eax], eax
  1764.         ret
  1765. ;--------------------------------------
  1766. @@:
  1767. ; F.26.9 - get the value of the time counter
  1768.         sub     ebx, 4
  1769.         jnz     @f
  1770.  
  1771.         mov     eax, [timer_ticks]
  1772.         mov     [esp + SYSCALL_STACK.eax], eax
  1773.         ret
  1774. ;--------------------------------------
  1775. @@:
  1776. ; F.26.10 - get the time from kernel launch in nanoseconds
  1777.         dec     ebx
  1778.         jnz     @f
  1779.  
  1780.         call    get_clock_ns
  1781.         mov     [esp + SYSCALL_STACK.edx], edx
  1782.         mov     [esp + SYSCALL_STACK.eax], eax
  1783.         ret
  1784. ;--------------------------------------
  1785. @@:
  1786. ; F.26.11 - Find out whether low-level HD access is enabled
  1787.         dec     ebx
  1788.         jnz     @f
  1789.  
  1790.         mov     eax, [lba_read_enabled]
  1791.         mov     [esp + SYSCALL_STACK.eax], eax
  1792.         ret
  1793. ;--------------------------------------
  1794. @@:
  1795. ; F.26.12 - Find out whether low-level PCI access is enabled
  1796.         dec     ebx
  1797.         jnz     .error
  1798.  
  1799.         mov     eax, [pci_access_enabled]
  1800.         mov     [esp + SYSCALL_STACK.eax], eax
  1801.         ret
  1802. ;--------------------------------------
  1803. .error:
  1804.         or      [esp + SYSCALL_STACK.eax], dword -1
  1805.         ret
  1806. ;-----------------------------------------------------------------------------
  1807. get_timer_ticks:
  1808.         mov     eax, [timer_ticks]
  1809.         ret
  1810. ;-----------------------------------------------------------------------------
  1811. iglobal
  1812. midi_base dw 0
  1813. endg
  1814.  
  1815. align 4
  1816. sys_midi:
  1817.         cmp     word [mididp], 0
  1818.         jnz     @f
  1819.         mov     [esp + SYSCALL_STACK.eax], 1
  1820.         ret
  1821. @@:
  1822.         and     [esp + SYSCALL_STACK.eax], 0
  1823.         dec     ebx
  1824.         jnz     .smn1
  1825.  ;    call setuart
  1826. @@:
  1827.         call    .is_output
  1828.         test    al, al
  1829.         jnz     @b
  1830.         mov     dx, word [midisp]
  1831.         mov     al, 0xff
  1832.         out     dx, al
  1833. @@:
  1834.         mov     dx, word [midisp]
  1835.         mov     al, 0xff
  1836.         out     dx, al
  1837.         call    .is_input
  1838.         test    al, al
  1839.         jnz     @b
  1840.         call    .get_mpu_in
  1841.         cmp     al, 0xfe
  1842.         jnz     @b
  1843. @@:
  1844.         call    .is_output
  1845.         test    al, al
  1846.         jnz     @b
  1847.         mov     dx, word [midisp]
  1848.         mov     al, 0x3f
  1849.         out     dx, al
  1850.         ret
  1851. .smn1:
  1852.         dec     ebx
  1853.         jnz     .ret
  1854. @@:
  1855.         call    .get_mpu_in
  1856.         call    .is_output
  1857.         test    al, al
  1858.         jnz     @b
  1859.         mov     al, bl
  1860.         call    .put_mpu_out
  1861. .ret:
  1862.         ret
  1863.  
  1864. .is_input:
  1865.         push    edx
  1866.         mov     dx, word [midisp]
  1867.         in      al, dx
  1868.         and     al, 0x80
  1869.         pop     edx
  1870.         ret
  1871.  
  1872. .is_output:
  1873.         push    edx
  1874.         mov     dx, word [midisp]
  1875.         in      al, dx
  1876.         and     al, 0x40
  1877.         pop     edx
  1878.         ret
  1879.  
  1880. .get_mpu_in:
  1881.         push    edx
  1882.         mov     dx, word [mididp]
  1883.         in      al, dx
  1884.         pop     edx
  1885.         ret
  1886.  
  1887. .put_mpu_out:
  1888.         push    edx
  1889.         mov     dx, word [mididp]
  1890.         out     dx, al
  1891.         pop     edx
  1892.         ret
  1893.  
  1894. ;-----------------------------------------------------------------------------
  1895. sys_end:
  1896.         cmp     [_display.select_cursor], 0
  1897.         je      @f
  1898. ; restore default cursor before killing
  1899.         pusha
  1900.         mov     ecx, [current_slot]
  1901.         call    restore_default_cursor_before_killing
  1902.         popa
  1903. @@:
  1904. ;--------------------------------------
  1905. ; kill all sockets this process owns
  1906.         pusha
  1907.         mov     edx, [current_slot]
  1908.         mov     edx, [edx + APPDATA.tid]
  1909.         call    socket_process_end
  1910.         popa
  1911. ;--------------------------------------
  1912.         mov     ecx, [current_slot]
  1913.         mov     eax, [ecx + APPDATA.tls_base]
  1914.         test    eax, eax
  1915.         jz      @F
  1916.  
  1917.         stdcall user_free, eax
  1918. @@:
  1919.  
  1920.         mov     eax, [current_slot]
  1921.         mov     [eax + APPDATA.state], TSTATE_ZOMBIE
  1922.         call    wakeup_osloop
  1923.  
  1924. .waitterm:            ; wait here for termination
  1925.         call    change_task
  1926.         jmp     .waitterm
  1927. ;------------------------------------------------------------------------------
  1928. align 4
  1929. restore_default_cursor_before_killing:
  1930.         pushfd
  1931.         cli
  1932.         mov     eax, [def_cursor]
  1933.         mov     [ecx + APPDATA.cursor], eax
  1934.  
  1935.         movzx   eax, word [MOUSE_Y]
  1936.         movzx   ebx, word [MOUSE_X]
  1937.         mov     eax, [d_width_calc_area + eax*4]
  1938.  
  1939.         add     eax, [_display.win_map]
  1940.         movzx   edx, byte [ebx + eax]
  1941.         shl     edx, BSF sizeof.APPDATA
  1942.         mov     esi, [SLOT_BASE + edx + APPDATA.cursor]
  1943.  
  1944.         cmp     esi, [current_cursor]
  1945.         je      @f
  1946.  
  1947.         push    esi
  1948.         call    [_display.select_cursor]
  1949.         mov     [current_cursor], esi
  1950. @@:
  1951.         mov     [redrawmouse_unconditional], 1
  1952.         call    wakeup_osloop
  1953.         popfd
  1954.         ret
  1955. ;------------------------------------------------------------------------------
  1956. iglobal
  1957. align 4
  1958. sys_system_table:
  1959.         dd      sysfn_deactivate        ; 1 = deactivate window
  1960.         dd      sysfn_terminate         ; 2 = terminate thread
  1961.         dd      sysfn_activate          ; 3 = activate window
  1962.         dd      sysfn_getidletime       ; 4 = get idle time
  1963.         dd      sysfn_getcpuclock       ; 5 = get cpu clock
  1964.         dd      sysfn_saveramdisk       ; 6 = save ramdisk
  1965.         dd      sysfn_getactive         ; 7 = get active window
  1966.         dd      sysfn_sound_flag        ; 8 = get/set sound_flag
  1967.         dd      sysfn_shutdown          ; 9 = shutdown with parameter
  1968.         dd      sysfn_minimize          ; 10 = minimize window
  1969.         dd      sysfn_getdiskinfo       ; 11 = get disk subsystem info
  1970.         dd      undefined_syscall       ; 12 = get last pressed key. function removed. sysfn_lastkey
  1971.         dd      sysfn_getversion        ; 13 = get kernel version
  1972.         dd      sysfn_waitretrace       ; 14 = wait retrace
  1973.         dd      sysfn_centermouse       ; 15 = center mouse cursor
  1974.         dd      sysfn_getfreemem        ; 16 = get free memory size
  1975.         dd      sysfn_getallmem         ; 17 = get total memory size
  1976.         dd      sysfn_terminate2        ; 18 = terminate thread using PID
  1977.                                         ;                 instead of slot
  1978.         dd      sysfn_mouse_acceleration; 19 = set/get mouse acceleration
  1979.         dd      sysfn_meminfo           ; 20 = get extended memory info
  1980.         dd      sysfn_pid_to_slot       ; 21 = get slot number for pid
  1981.         dd      sysfn_min_rest_window   ; 22 = minimize and restore any window
  1982.         dd      sysfn_min_windows       ; 23 = minimize all windows
  1983.         dd      sysfn_set_screen_sizes  ; 24 = set screen sizes for Vesa
  1984.  
  1985.         dd      sysfn_zmodif            ; 25 = get/set window z modifier  ;Fantomer
  1986. sysfn_num = ($ - sys_system_table)/4
  1987. endg
  1988. ;------------------------------------------------------------------------------
  1989. sys_system:
  1990.         dec     ebx
  1991.         cmp     ebx, sysfn_num
  1992.         jae     @f
  1993.         jmp     dword [sys_system_table + ebx*4]
  1994. @@:
  1995.         ret
  1996. ;------------------------------------------------------------------------------
  1997. sysfn_shutdown:          ; 18.9 = system shutdown
  1998.         cmp     ecx, SYSTEM_SHUTDOWN
  1999.         jl      exit_for_anyone
  2000.         cmp     ecx, SYSTEM_RESTART
  2001.         jg      exit_for_anyone
  2002.         mov     [BOOT.shutdown_type], cl
  2003.  
  2004.         mov     eax, [thread_count]
  2005.         mov     [SYS_SHUTDOWN], al
  2006.         mov     [shutdown_processes], eax
  2007.         call    wakeup_osloop
  2008.         and     dword [esp + SYSCALL_STACK.eax], 0
  2009.  exit_for_anyone:
  2010.         ret
  2011.   uglobal
  2012.    shutdown_processes:
  2013.                        dd 0x0
  2014.   endg
  2015. ;------------------------------------------------------------------------------
  2016. ; in: eax -- APPDATA ptr
  2017. ; out: Z/z -- is/not kernel thread
  2018. is_kernel_thread:
  2019.         mov     eax, [eax + APPDATA.process]
  2020.         cmp     eax, [SLOT_BASE + 2*sizeof.APPDATA + APPDATA.process]       ; OS
  2021.         ret
  2022. ;------------------------------------------------------------------------------
  2023. sysfn_terminate:        ; 18.2 = TERMINATE
  2024.         push    ecx
  2025.         cmp     ecx, 2
  2026.         jb      .noprocessterminate
  2027.         mov     edx, [thread_count]
  2028.         cmp     ecx, edx
  2029.         ja      .noprocessterminate
  2030.         mov     eax, [thread_count]
  2031.         shl     ecx, BSF sizeof.APPDATA
  2032.         add     ecx, SLOT_BASE
  2033.  
  2034.         mov     edx, [ecx + APPDATA.tid]
  2035.         cmp     byte [ecx + APPDATA.state], TSTATE_FREE
  2036.         jz      .noprocessterminate
  2037.         push    eax
  2038.         mov     eax, ecx
  2039.         call    is_kernel_thread
  2040.         pop     eax
  2041.         jz      .noprocessterminate
  2042.         push    ecx edx
  2043.         mov     edx, ecx
  2044.         call    request_terminate
  2045.         pop     edx ecx
  2046.         test    eax, eax
  2047.         jz      .noprocessterminate
  2048. ;--------------------------------------
  2049. ; terminate all network sockets it used
  2050.         pusha
  2051.         mov     eax, edx     ;TODO: check function
  2052.         call    socket_process_end
  2053.         popa
  2054. ;--------------------------------------
  2055.         cmp     [_display.select_cursor], 0
  2056.         je      .restore_end
  2057. ; restore default cursor before killing
  2058.         pusha
  2059.         mov     ecx, [esp+32]
  2060.         shl     ecx, BSF sizeof.APPDATA
  2061.         add     ecx, SLOT_BASE
  2062.         mov     eax, [def_cursor]
  2063.         cmp     [ecx + APPDATA.cursor], eax
  2064.         je      @f
  2065.         call    restore_default_cursor_before_killing
  2066. @@:
  2067.         popa
  2068. .restore_end:
  2069. ;--------------------------------------
  2070.      ;call MEM_Heap_Lock      ;guarantee that process isn't working with heap
  2071.         mov     [ecx + APPDATA.state], TSTATE_ZOMBIE; clear possible i40's
  2072.         call    wakeup_osloop
  2073.      ;call MEM_Heap_UnLock
  2074.  
  2075.         cmp     edx, [application_table_owner]; clear app table stat
  2076.         jne     .noatsc
  2077.         call    unlock_application_table
  2078. .noatsc:
  2079. .noprocessterminate:
  2080.         add     esp, 4
  2081.         ret
  2082. ;------------------------------------------------------------------------------
  2083. sysfn_terminate2:
  2084.         call    lock_application_table
  2085.         mov     eax, ecx
  2086.         call    pid_to_slot
  2087.         test    eax, eax
  2088.         jz      .not_found
  2089.         mov     ecx, eax
  2090.         cli
  2091.         call    sysfn_terminate
  2092.         call    unlock_application_table
  2093.         sti
  2094.         and     dword [esp + SYSCALL_STACK.eax], 0
  2095.         ret
  2096. .not_found:
  2097.         call    unlock_application_table
  2098.         or      dword [esp + SYSCALL_STACK.eax], -1
  2099.         ret
  2100. ;------------------------------------------------------------------------------
  2101. sysfn_deactivate:         ; 18.1 = DEACTIVATE WINDOW
  2102.         cmp     ecx, 2
  2103.         jb      .nowindowdeactivate
  2104.         cmp     ecx, [thread_count]
  2105.         ja      .nowindowdeactivate
  2106.  
  2107.         movzx   esi, word [WIN_STACK + ecx*2]
  2108.         cmp     esi, 1
  2109.         je      .nowindowdeactivate ; already deactive
  2110.  
  2111.         mov     edi, ecx
  2112.         shl     edi, 5
  2113.         add     edi, window_data
  2114.         movzx   esi, word [WIN_STACK + ecx * 2]
  2115.         lea     esi, [WIN_POS + esi * 2]
  2116.         call    window._.window_deactivate
  2117.         call    syscall_display_settings.calculateScreen
  2118.         call    syscall_display_settings.redrawScreen
  2119. .nowindowdeactivate:
  2120.         ret
  2121. ;------------------------------------------------------------------------------
  2122. sysfn_activate:         ; 18.3 = ACTIVATE WINDOW
  2123.         cmp     ecx, 2
  2124.         jb      .nowindowactivate
  2125.         cmp     ecx, [thread_count]
  2126.         ja      .nowindowactivate
  2127. ;-------------------------------------
  2128. @@:
  2129. ; If the window is captured and moved by the user,
  2130. ; then you can't change the position in window stack!!!
  2131.         mov     al, [mouse.active_sys_window.action]
  2132.         and     al, WINDOW_MOVE_AND_RESIZE_FLAGS
  2133.         test    al, al
  2134.         jz      @f
  2135.         call    change_task
  2136.         jmp     @b
  2137. @@:
  2138. ;-------------------------------------
  2139.         mov     [window_minimize], 2; restore window if minimized
  2140.         call    wakeup_osloop
  2141.  
  2142.         movzx   esi, word [WIN_STACK + ecx*2]
  2143.         cmp     esi, [thread_count]
  2144.         je      .nowindowactivate; already active
  2145.  
  2146.         mov     edi, ecx
  2147.         shl     edi, BSF sizeof.WDATA
  2148.         add     edi, window_data
  2149.         movzx   esi, word [WIN_STACK + ecx * 2]
  2150.         lea     esi, [WIN_POS + esi * 2]
  2151.         call    waredraw
  2152. .nowindowactivate:
  2153.         ret
  2154. ;------------------------------------------------------------------------------
  2155. align 4
  2156. sysfn_zmodif:
  2157. ;18,25,1 - get z_modif
  2158. ;18,25,2 - set z_modif
  2159. ;edx = -1(for current task) or TID
  2160. ;esi(for 2) = new value z_modif
  2161. ;return:
  2162. ;1:   eax = z_modif
  2163. ;2: eax=0(fail),1(success) for set z_modif
  2164.  
  2165.         cmp     edx, -1
  2166.         jne     @f
  2167.         mov     edx, [current_slot_idx]
  2168.      @@:
  2169.         cmp     edx, [thread_count]
  2170.         ja      .fail
  2171.         cmp     edx, 1
  2172.         je      .fail
  2173.  
  2174.         mov     eax, edx
  2175.         shl     edx, BSF sizeof.WDATA
  2176.  
  2177.         cmp     [edx*8 + SLOT_BASE + APPDATA.state], TSTATE_FREE
  2178.         je      .fail
  2179.  
  2180.         cmp     ecx, 1
  2181.         jnz     .set_zmod
  2182.  
  2183.         mov     al, [window_data + edx + WDATA.z_modif]
  2184.         jmp     .exit
  2185.  
  2186. .set_zmod:
  2187.         cmp     ecx, 2
  2188.         jnz     .fail
  2189.  
  2190.         mov     ebx, esi
  2191.         mov     esi, eax
  2192.  
  2193.         cmp     bl, ZPOS_ALWAYS_TOP
  2194.         jg      .fail
  2195.  
  2196.         mov     [window_data + edx + WDATA.z_modif], bl
  2197.  
  2198.         mov     eax, [window_data + edx + WDATA.box.left]
  2199.         mov     ebx, [window_data + edx + WDATA.box.top]
  2200.         mov     ecx, [window_data + edx + WDATA.box.width]
  2201.         mov     edx, [window_data + edx + WDATA.box.height]
  2202.         add     ecx, eax
  2203.         add     edx, ebx
  2204.         call    window._.set_screen
  2205.         call    window._.set_top_wnd
  2206.         call    window._.redraw_top_wnd
  2207.  
  2208.         shl     esi, BSF sizeof.WDATA
  2209.         mov     [esi + window_data + WDATA.fl_redraw], 1
  2210.  
  2211.  
  2212.         mov     eax, 1
  2213.         jmp     .exit
  2214. .fail:
  2215.         xor     eax, eax
  2216. .exit:
  2217.         mov     [esp + SYSCALL_STACK.eax], eax
  2218.         ret
  2219.  
  2220. ;------------------------------------------------------------------------------
  2221. sysfn_getidletime:              ; 18.4 = GET IDLETIME
  2222.         ;mov     eax, [TASK_TABLE+32+TASKDATA.cpu_usage]
  2223.         mov     eax, [SLOT_BASE + APPDATA.cpu_usage]
  2224.         mov     [esp + SYSCALL_STACK.eax], eax
  2225.         ret
  2226. ;------------------------------------------------------------------------------
  2227. sysfn_getcpuclock:              ; 18.5 = GET TSC/SEC
  2228.         mov     eax, dword [cpu_freq]
  2229.         mov     [esp + SYSCALL_STACK.eax], eax
  2230.         ret
  2231. ;------------------------------------------------------------------------------
  2232. get_cpu_freq:
  2233.         mov     eax, dword [cpu_freq]
  2234.         mov     edx, dword [cpu_freq+4]
  2235.         ret
  2236. ;  SAVE ramdisk to /hd/1/kolibri.img
  2237. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2238.    include 'blkdev/rdsave.inc'
  2239. ;!!!!!!!!!!!!!!!!!!!!!!!!
  2240. ;------------------------------------------------------------------------------
  2241. align 4
  2242. sysfn_getactive:        ; 18.7 = get active window
  2243.         mov     eax, [thread_count]
  2244.         movzx   eax, word [WIN_POS + eax*2]
  2245.         mov     [esp + SYSCALL_STACK.eax], eax
  2246.         ret
  2247. ;------------------------------------------------------------------------------
  2248. sysfn_sound_flag:       ; 18.8 = get/set sound_flag
  2249. ;     cmp  ecx,1
  2250.         dec     ecx
  2251.         jnz     .set_flag
  2252.         movzx   eax, byte [sound_flag]; get sound_flag
  2253.         mov     [esp + SYSCALL_STACK.eax], eax
  2254.         ret
  2255. .set_flag:
  2256. ;     cmp  ecx,2
  2257.         dec     ecx
  2258.         jnz     .err
  2259.         xor     byte [sound_flag], 1
  2260. .err:
  2261.         ret
  2262. ;------------------------------------------------------------------------------
  2263. sysfn_minimize:         ; 18.10 = minimize window
  2264.         mov     [window_minimize], 1
  2265.         call    wakeup_osloop
  2266.         ret
  2267. ;------------------------------------------------------------------------------
  2268. align 4
  2269. sysfn_getdiskinfo:      ; 18.11 = get disk info table
  2270.         dec     ecx
  2271.         jnz     .exit
  2272. .small_table:
  2273.         stdcall is_region_userspace, edx, DRIVE_DATA_SIZE
  2274.         jnz     .exit
  2275.         mov     edi, edx
  2276.         mov     esi, DRIVE_DATA
  2277.         mov     ecx, DRIVE_DATA_SIZE ;10
  2278.         cld
  2279.         rep movsb
  2280. .exit:
  2281.         ret
  2282. ;------------------------------------------------------------------------------
  2283. sysfn_getversion:       ; 18.13 = get kernel ID and version
  2284.         ; if given memory address belongs to kernel then error
  2285.         stdcall is_region_userspace, ecx, version_end-version_inf
  2286.         jnz     .addr_error
  2287.  
  2288.         mov     edi, ecx
  2289.         mov     esi, version_inf
  2290.         mov     ecx, version_end-version_inf
  2291.         rep movsb
  2292.         ret
  2293. .addr_error:    ; if given memory address is illegal
  2294.         mov     dword [esp + SYSCALL_STACK.eax], -1
  2295.         ret  
  2296. ;------------------------------------------------------------------------------
  2297. sysfn_waitretrace:     ; 18.14 = sys wait retrace
  2298.         ;wait retrace functions
  2299.         mov     edx, 0x3da
  2300. .loop:
  2301.         in      al, dx
  2302.         test    al, 1000b
  2303.         jz      .loop
  2304.         and     [esp + SYSCALL_STACK.eax], dword 0
  2305.         ret
  2306. ;------------------------------------------------------------------------------
  2307. align 4
  2308. sysfn_centermouse:      ; 18.15 = mouse centered
  2309.         mov     eax, [_display.width]
  2310.         shr     eax, 1
  2311.         mov     [MOUSE_X], ax
  2312.         mov     eax, [_display.height]
  2313.         shr     eax, 1
  2314.         mov     [MOUSE_Y], ax
  2315.         call    wakeup_osloop
  2316.         xor     eax, eax
  2317.         and     [esp + SYSCALL_STACK.eax], eax
  2318.         ret
  2319. ;------------------------------------------------------------------------------
  2320. sysfn_mouse_acceleration:       ; 18.19 = set/get mouse features
  2321.         cmp     ecx, 8
  2322.         jnc     @f
  2323.         jmp     dword [.table + ecx*4]
  2324. .get_mouse_acceleration:
  2325.         xor     eax, eax
  2326.         mov     ax, [mouse_speed_factor]
  2327.         mov     [esp + SYSCALL_STACK.eax], eax
  2328.         ret
  2329. .set_mouse_acceleration:
  2330.         mov     [mouse_speed_factor], dx
  2331.         ret
  2332. .get_mouse_delay:
  2333.         xor     eax, eax
  2334.         mov     al, [mouse_delay]
  2335.         mov     [esp + SYSCALL_STACK.eax], eax
  2336.         ret
  2337. .set_mouse_delay:
  2338.         mov     [mouse_delay], dl
  2339. @@:
  2340.         ret
  2341. .set_pointer_position:
  2342.         cmp     dx, word[_display.height]
  2343.         jae     @b
  2344.         rol     edx, 16
  2345.         cmp     dx, word[_display.width]
  2346.         jae     @b
  2347.         mov     [MOUSE_X], edx
  2348.         mov     [mouse_active], 1
  2349.         jmp     wakeup_osloop
  2350. .set_mouse_button:
  2351.         mov     [BTN_DOWN], edx
  2352.         mov     [mouse_active], 1
  2353.         jmp     wakeup_osloop
  2354. .get_doubleclick_delay:
  2355.         xor     eax, eax
  2356.         mov     al, [mouse_doubleclick_delay]
  2357.         mov     [esp + SYSCALL_STACK.eax], eax
  2358.         ret
  2359. .set_doubleclick_delay:
  2360.         mov     [mouse_doubleclick_delay], dl
  2361.         ret
  2362. align 4
  2363. .table:
  2364. dd      .get_mouse_acceleration
  2365. dd      .set_mouse_acceleration
  2366. dd      .get_mouse_delay
  2367. dd      .set_mouse_delay
  2368. dd      .set_pointer_position
  2369. dd      .set_mouse_button
  2370. dd      .get_doubleclick_delay
  2371. dd      .set_doubleclick_delay
  2372. ;------------------------------------------------------------------------------
  2373. sysfn_getfreemem:
  2374.         mov     eax, [pg_data.pages_free]
  2375.         shl     eax, 2
  2376.         mov     [esp + SYSCALL_STACK.eax], eax
  2377.         ret
  2378. ;------------------------------------------------------------------------------
  2379. sysfn_getallmem:
  2380.         mov     eax, [MEM_AMOUNT]
  2381.         shr     eax, 10
  2382.         mov     [esp + SYSCALL_STACK.eax], eax
  2383.         ret
  2384. ;------------------------------------------------------------------------------
  2385. sysfn_pid_to_slot:
  2386.         mov     eax, ecx
  2387.         call    pid_to_slot
  2388.         mov     [esp + SYSCALL_STACK.eax], eax
  2389.         ret
  2390. ;------------------------------------------------------------------------------
  2391. sysfn_min_rest_window:
  2392.         pushad
  2393.         mov     eax, edx ; ebx - operating
  2394.         shr     ecx, 1
  2395.         jnc     @f
  2396.         call    pid_to_slot
  2397. @@:
  2398.         or      eax, eax ; eax - number of slot
  2399.         jz      .error
  2400.         cmp     eax, max_processes    ; varify maximal slot number
  2401.         ja      .error
  2402.         movzx   eax, word [WIN_STACK + eax*2]
  2403.         shr     ecx, 1
  2404.         jc      .restore
  2405.  ; .minimize:
  2406.         call    minimize_window
  2407.         jmp     .exit
  2408. .restore:
  2409.         call    restore_minimized_window
  2410. .exit:
  2411.         popad
  2412.         xor     eax, eax
  2413.         mov     [esp + SYSCALL_STACK.eax], eax
  2414.         ret
  2415. .error:
  2416.         popad
  2417.         xor     eax, eax
  2418.         dec     eax
  2419.         mov     [esp + SYSCALL_STACK.eax], eax
  2420.         ret
  2421. ;------------------------------------------------------------------------------
  2422. sysfn_min_windows:
  2423.         call    minimize_all_window
  2424.         mov     [esp + SYSCALL_STACK.eax], eax
  2425.         call    change_task
  2426.         ret
  2427. ;------------------------------------------------------------------------------
  2428. sysfn_set_screen_sizes:
  2429.         cmp     [SCR_MODE], word 0x13
  2430.         jbe     .exit
  2431.  
  2432.         cmp     [_display.select_cursor], select_cursor
  2433.         jne     .exit
  2434.  
  2435.         cmp     ecx, [display_width_standard]
  2436.         ja      .exit
  2437.  
  2438.         cmp     edx, [display_height_standard]
  2439.         ja      .exit
  2440.  
  2441.         pushfd
  2442.         cli
  2443.         mov     eax, ecx
  2444.         mov     ecx, [_display.lfb_pitch]
  2445.         mov     [_display.width], eax
  2446.         dec     eax
  2447.         mov     [_display.height], edx
  2448.         dec     edx
  2449. ; eax - new Screen_Max_X
  2450. ; edx - new Screen_Max_Y
  2451.         mov     [do_not_touch_winmap], 1
  2452.         call    set_screen
  2453.         mov     [do_not_touch_winmap], 0
  2454.         popfd
  2455.         call    change_task
  2456. .exit:
  2457.         ret
  2458. ;------------------------------------------------------------------------------
  2459. uglobal
  2460. screen_workarea RECT
  2461. display_width_standard dd 0
  2462. display_height_standard dd 0
  2463. do_not_touch_winmap db 0
  2464. window_minimize db 0
  2465. sound_flag      db 0
  2466.  
  2467. endg
  2468.  
  2469. ;UID_NONE=0
  2470. ;UID_KOLIBRI=2    ;russian
  2471.  
  2472. iglobal
  2473. version_inf:
  2474.         db 0,7,7,0  ; version 0.7.7.0
  2475.         db 0
  2476. .rev    dd __REV__
  2477. version_end:
  2478. endg
  2479. ;------------------------------------------------------------------------------
  2480. align 4
  2481. sys_cachetodiskette:
  2482.         cmp     ebx, 1
  2483.         jb      .no_floppy_save
  2484.         cmp     ebx, 2
  2485.         ja      .no_floppy_save
  2486.         call    save_image
  2487.         mov     [esp + SYSCALL_STACK.eax], eax
  2488.         ret
  2489. .no_floppy_save:
  2490.         mov     [esp + SYSCALL_STACK.eax], dword 1
  2491.         ret
  2492. ;------------------------------------------------------------------------------
  2493. align 4
  2494. sys_cpuusage:
  2495.  
  2496. ;  RETURN:
  2497. ;
  2498. ;  +00 dword     process cpu usage
  2499. ;  +04  word     position in windowing stack
  2500. ;  +06  word     windowing stack value at current position (cpu nro)
  2501. ;  +10 12 bytes  name
  2502. ;  +22 dword     start in mem
  2503. ;  +26 dword     used mem
  2504. ;  +30 dword     PID , process idenfification number
  2505. ;
  2506.         ; if given memory address belongs to kernel then error
  2507.         stdcall is_region_userspace, ebx, 0x4C
  2508.         jnz     .addr_error
  2509.  
  2510.         cmp     ecx, -1 ; who am I ?
  2511.         jne     .no_who_am_i
  2512.         mov     ecx, [current_slot_idx]
  2513.   .no_who_am_i:
  2514.         cmp     ecx, max_processes
  2515.         ja      .nofillbuf
  2516.  
  2517. ; +4: word: position of the window of thread in the window stack
  2518.         mov     ax, [WIN_STACK + ecx * 2]
  2519.         mov     [ebx+4], ax
  2520. ; +6: word: number of the thread slot, which window has in the window stack
  2521. ;           position ecx (has no relation to the specific thread)
  2522.         mov     ax, [WIN_POS + ecx * 2]
  2523.         mov     [ebx+6], ax
  2524.  
  2525.         shl     ecx, BSF sizeof.APPDATA
  2526.  
  2527. ; +0: dword: memory usage
  2528.         ;mov     eax, [ecx+TASK_TABLE+TASKDATA.cpu_usage]
  2529.         mov     eax, [SLOT_BASE + ecx - sizeof.APPDATA + APPDATA.cpu_usage]
  2530.         mov     [ebx], eax
  2531. ; +10: 11 bytes: name of the process
  2532.         push    ecx
  2533.         lea     eax, [SLOT_BASE + ecx + APPDATA.app_name]
  2534.         add     ebx, 10
  2535.         mov     ecx, 11
  2536.         call    memmove
  2537.         pop     ecx
  2538.  
  2539.         shr     ecx, 3
  2540. ; +22: address of the process in memory
  2541. ; +26: size of used memory - 1
  2542.         push    edi
  2543.         lea     edi, [ebx+12]
  2544.         xor     eax, eax
  2545.         mov     edx, 0x100000*16
  2546.         cmp     ecx, 1 shl 5
  2547.         je      .os_mem
  2548.         mov     edx, [SLOT_BASE + ecx*8 + APPDATA.process]
  2549.         mov     edx, [edx + PROC.mem_used]
  2550.         mov     eax, std_application_base_address
  2551. .os_mem:
  2552.         stosd
  2553.         lea     eax, [edx-1]
  2554.         stosd
  2555.  
  2556. ; +30: PID/TID
  2557.         mov     eax, [SLOT_BASE + ecx*8 + APPDATA.tid]
  2558.         stosd
  2559.  
  2560.     ; window position and size
  2561.         push    esi
  2562.         lea     esi, [window_data + ecx + WDATA.box]
  2563.         movsd
  2564.         movsd
  2565.         movsd
  2566.         movsd
  2567.  
  2568.     ; Process state (+50)
  2569.         movzx   eax, byte [SLOT_BASE + ecx*8 + APPDATA.state]
  2570.         stosd
  2571.  
  2572.     ; Window client area box
  2573.         lea     esi, [SLOT_BASE + ecx*8 + APPDATA.wnd_clientbox]
  2574.         movsd
  2575.         movsd
  2576.         movsd
  2577.         movsd
  2578.  
  2579.     ; Window state
  2580.         mov     al, [window_data + ecx + WDATA.fl_wstate]
  2581.         stosb
  2582.  
  2583.     ; Event mask (+71)
  2584.         mov     EAX, dword [SLOT_BASE + ecx*8 + APPDATA.event_mask]
  2585.         stosd
  2586.  
  2587.     ; Keyboard mode (+75)
  2588.         mov     al, byte [SLOT_BASE + ecx*8 + APPDATA.keyboard_mode]
  2589.         stosb
  2590.  
  2591.         pop     esi
  2592.         pop     edi
  2593.  
  2594. .nofillbuf:
  2595.     ; return number of processes
  2596.  
  2597.         mov     eax, [thread_count]
  2598.         mov     [esp + SYSCALL_STACK.eax], eax
  2599.         ret
  2600.  
  2601. .addr_error:    ; if given memory address is illegal
  2602.         mov     dword [esp + SYSCALL_STACK.eax], -1
  2603.         ret  
  2604.  
  2605.  
  2606. ; redraw status
  2607. align 4
  2608. sys_redrawstat:
  2609.         cmp     ebx, 1
  2610.         jne     .no_widgets_away
  2611.         ; buttons away
  2612.         mov     ecx, [current_slot_idx]
  2613. .sys_newba2:
  2614.         mov     edi, [BTN_ADDR]
  2615.         cmp     [edi], dword 0  ; empty button list ?
  2616.         je      .end_of_buttons_away
  2617.         movzx   ebx, word [edi]
  2618.         inc     ebx
  2619.         mov     eax, edi
  2620. .sys_newba:
  2621.         dec     ebx
  2622.         jz      .end_of_buttons_away
  2623.  
  2624.         add     eax, 0x10
  2625.         cmp     cx, [eax]
  2626.         jnz     .sys_newba
  2627.  
  2628.         push    eax ebx ecx
  2629.         mov     ecx, ebx
  2630.         inc     ecx
  2631.         shl     ecx, 4
  2632.         mov     ebx, eax
  2633.         add     eax, 0x10
  2634.         call    memmove
  2635.         dec     dword [edi]
  2636.         pop     ecx ebx eax
  2637.  
  2638.         jmp     .sys_newba2
  2639.  
  2640. .end_of_buttons_away:
  2641.         ret
  2642.  
  2643. .no_widgets_away:
  2644.  
  2645.         cmp     ebx, 2
  2646.         jnz     .srl1
  2647.  
  2648.         mov     edx, [current_slot_idx]      ; return whole screen draw area for this app
  2649.         shl     edx, 5     ;?
  2650.         add     edx, draw_data
  2651.         mov     [edx + RECT.left], 0
  2652.         mov     [edx + RECT.top], 0
  2653.         mov     eax, [_display.width]
  2654.         dec     eax
  2655.         mov     [edx + RECT.right], eax
  2656.         mov     eax, [_display.height]
  2657.         dec     eax
  2658.         mov     [edx + RECT.bottom], eax
  2659.  
  2660. .srl1:
  2661.         ret
  2662.  
  2663. ;ok - 100% work
  2664. ;nt - not tested
  2665. ;---------------------------------------------------------------------------------------------
  2666. ;eax
  2667. ;0 - task switch counter. Ret switch counter in eax. Block. ok.
  2668. ;1 - change task. Ret nothing. Block. ok.
  2669. ;2 - performance control
  2670. ; ebx
  2671. ; 0 - enable or disable (inversion) PCE flag on CR4 for rdmpc in user mode.
  2672. ; returned new cr4 in eax. Ret cr4 in eax. Block. ok.
  2673. ; 1 - is cache enabled. Ret cr0 in eax if enabled else zero in eax. Block. ok.
  2674. ; 2 - enable cache. Ret 1 in eax. Ret nothing. Block. ok.
  2675. ; 3 - disable cache. Ret 0 in eax. Ret nothing. Block. ok.
  2676. ;eax
  2677. ;3 - rdmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2678. ;4 - wrmsr. Counter in edx. (edx:eax) [esi:edi, edx] => [edx:esi, ecx]. Ret in ebx:eax. Block. ok.
  2679. ;---------------------------------------------------------------------------------------------
  2680. iglobal
  2681. align 4
  2682. sheduler:
  2683.         dd      sys_sheduler.00
  2684.         dd      change_task
  2685.         dd      sys_sheduler.02
  2686.         dd      sys_sheduler.03
  2687.         dd      sys_sheduler.04
  2688. endg
  2689. sys_sheduler:
  2690. ;rewritten by <Lrz>  29.12.2009
  2691.         jmp     dword [sheduler + ebx*4]
  2692. ;.shed_counter:
  2693. .00:
  2694.         mov     eax, [context_counter]
  2695.         mov     [esp + SYSCALL_STACK.eax], eax
  2696.         ret
  2697.  
  2698. .02:
  2699. ;.perf_control:
  2700.         inc     ebx                     ;before ebx=2, ebx=3
  2701.         cmp     ebx, ecx                ;if ecx=3, ebx=3
  2702.         jz      cache_disable
  2703.  
  2704.         dec     ebx                     ;ebx=2
  2705.         cmp     ebx, ecx                ;
  2706.         jz      cache_enable            ;if ecx=2 and ebx=2
  2707.  
  2708.         dec     ebx                     ;ebx=1
  2709.         cmp     ebx, ecx
  2710.         jz      is_cache_enabled        ;if ecx=1 and ebx=1
  2711.  
  2712.         dec     ebx
  2713.         test    ebx, ecx                ;ebx=0 and ecx=0
  2714.         jz      modify_pce              ;if ecx=0
  2715.  
  2716.         ret
  2717.  
  2718. .03:
  2719. ;.rdmsr_instr:
  2720. ;now counter in ecx
  2721. ;(edx:eax) esi:edi => edx:esi
  2722.         mov     eax, esi
  2723.         mov     ecx, edx
  2724.         rdmsr
  2725.         mov     [esp + SYSCALL_STACK.eax], eax
  2726.         mov     [esp + SYSCALL_STACK.ebx], edx           ;ret in ebx?
  2727.         ret
  2728.  
  2729. .04:
  2730. ;.wrmsr_instr:
  2731. ;now counter in ecx
  2732. ;(edx:eax) esi:edi => edx:esi
  2733.         ; Fast Call MSR can't be destroy
  2734.         ; Но MSR_AMD_EFER можно изменять, т.к. в этом регистре лиш
  2735.         ; включаются/выключаются расширенные возможности
  2736.         cmp     edx, MSR_SYSENTER_CS
  2737.         je      @f
  2738.         cmp     edx, MSR_SYSENTER_ESP
  2739.         je      @f
  2740.         cmp     edx, MSR_SYSENTER_EIP
  2741.         je      @f
  2742.         cmp     edx, MSR_AMD_STAR
  2743.         je      @f
  2744.  
  2745.         mov     eax, esi
  2746.         mov     ecx, edx
  2747.         wrmsr
  2748.         ; mov   [esp + 32], eax
  2749.         ; mov   [esp + 20], edx ;ret in ebx?
  2750. @@:
  2751.         ret
  2752.  
  2753. cache_disable:
  2754.         mov     eax, cr0
  2755.         or      eax, 01100000_00000000_00000000_00000000b
  2756.         mov     cr0, eax
  2757.         wbinvd  ;set MESI
  2758.         ret
  2759.  
  2760. cache_enable:
  2761.         mov     eax, cr0
  2762.         and     eax, 10011111_11111111_11111111_11111111b
  2763.         mov     cr0, eax
  2764.         ret
  2765.  
  2766. is_cache_enabled:
  2767.         mov     eax, cr0
  2768.         mov     ebx, eax
  2769.         and     eax, 01100000_00000000_00000000_00000000b
  2770.         jz      cache_disabled
  2771.         mov     [esp + SYSCALL_STACK.eax], ebx
  2772. cache_disabled:
  2773.         mov     dword [esp + SYSCALL_STACK.eax], eax;0
  2774.         ret
  2775.  
  2776. modify_pce:
  2777.         mov     eax, cr4
  2778. ;       mov ebx,0
  2779. ;       or  bx,100000000b ;pce
  2780. ;       xor eax,ebx ;invert pce
  2781.         bts     eax, 8;pce=cr4[8]
  2782.         mov     cr4, eax
  2783.         mov     [esp + SYSCALL_STACK.eax], eax
  2784.         ret
  2785. ;---------------------------------------------------------------------------------------------
  2786.  
  2787.  
  2788. iglobal
  2789.   cpustring db 'CPU',0
  2790. endg
  2791.  
  2792. uglobal
  2793. background_defined    db    0    ; diamond, 11.04.2006
  2794. endg
  2795. ;-----------------------------------------------------------------------------
  2796. align 4
  2797. checkmisc:
  2798.         cmp     [ctrl_alt_del], 1
  2799.         jne     .nocpustart
  2800.  
  2801.         mov     ebp, cpustring
  2802.         call    fs_execute_from_sysdir
  2803.  
  2804.         mov     [ctrl_alt_del], 0
  2805. ;--------------------------------------
  2806. align 4
  2807. .nocpustart:
  2808.         cmp     [mouse_active], 1
  2809.         jne     .mouse_not_active
  2810.         mov     [mouse_active], 0
  2811.  
  2812.         xor     edi, edi
  2813.         mov     ebx, window_data
  2814.  
  2815.         mov     ecx, [thread_count]
  2816.         movzx   eax, word [WIN_POS + ecx*2]     ; active window
  2817.         shl     eax, 8
  2818.         push    eax
  2819.  
  2820.         movzx   eax, word [MOUSE_X]
  2821.         movzx   edx, word [MOUSE_Y]
  2822. ;--------------------------------------
  2823. align 4
  2824. .set_mouse_event:
  2825.         add     edi, sizeof.APPDATA
  2826.         add     ebx, sizeof.WDATA
  2827.         test    [SLOT_BASE + edi + APPDATA.event_mask], 0x80000000
  2828.         jz      .pos_filter
  2829.  
  2830.         cmp     edi, [esp]                      ; skip if filtration active
  2831.         jne     .skip
  2832. ;--------------------------------------
  2833. align 4
  2834. .pos_filter:
  2835.         test    [SLOT_BASE + edi + APPDATA.event_mask], 0x40000000
  2836.         jz      .set
  2837.  
  2838.         mov     esi, [ebx + WDATA.box.left]
  2839.         cmp     eax, esi
  2840.         jb      .skip
  2841.         add     esi, [ebx + WDATA.box.width]
  2842.         cmp     eax, esi
  2843.         ja      .skip
  2844.  
  2845.         mov     esi, [ebx + WDATA.box.top]
  2846.         cmp     edx, esi
  2847.         jb      .skip
  2848.         add     esi, [ebx + WDATA.box.height]
  2849.         cmp     edx, esi
  2850.         ja      .skip
  2851. ;--------------------------------------
  2852. align 4
  2853. .set:
  2854.         or      [SLOT_BASE + edi + APPDATA.occurred_events], EVENT_MOUSE
  2855. ;--------------------------------------
  2856. align 4
  2857. .skip:
  2858.         loop    .set_mouse_event
  2859.  
  2860.         pop     eax
  2861. ;--------------------------------------
  2862. align 4
  2863. .mouse_not_active:
  2864.         cmp     [REDRAW_BACKGROUND], 0  ; background update ?
  2865.         jz      nobackgr
  2866.  
  2867.         cmp     [background_defined], 0
  2868.         jz      nobackgr
  2869. ;--------------------------------------
  2870. align 4
  2871. backgr:
  2872.         mov     eax, [draw_data + 32 + RECT.left]
  2873.         shl     eax, 16
  2874.         add     eax, [draw_data + 32 + RECT.right]
  2875.         mov     [BG_Rect_X_left_right], eax ; [left]*65536 + [right]
  2876.  
  2877.         mov     eax, [draw_data + 32 + RECT.top]
  2878.         shl     eax, 16
  2879.         add     eax, [draw_data + 32 + RECT.bottom]
  2880.         mov     [BG_Rect_Y_top_bottom], eax ; [top]*65536 + [bottom]
  2881.  
  2882.         call    drawbackground
  2883. ;        DEBUGF  1, "K : drawbackground\n"
  2884. ;        DEBUGF  1, "K : backg x %x\n",[BG_Rect_X_left_right]
  2885. ;        DEBUGF  1, "K : backg y %x\n",[BG_Rect_Y_top_bottom]
  2886. ;--------- set event 5 start ----------
  2887.         push    ecx edi
  2888.         xor     edi, edi
  2889.         mov     ecx, [thread_count]
  2890. ;--------------------------------------
  2891. align 4
  2892. set_bgr_event:
  2893.         add     edi, sizeof.APPDATA
  2894.         mov     eax, [BG_Rect_X_left_right]
  2895.         mov     edx, [BG_Rect_Y_top_bottom]
  2896.         cmp     [SLOT_BASE + edi + APPDATA.draw_bgr_x], 0
  2897.         jz      .set
  2898. .join:
  2899.         cmp     word [SLOT_BASE + edi + APPDATA.draw_bgr_x], ax
  2900.         jae     @f
  2901.         mov     word [SLOT_BASE + edi + APPDATA.draw_bgr_x], ax
  2902. @@:
  2903.         shr     eax, 16
  2904.         cmp     word [SLOT_BASE + edi + APPDATA.draw_bgr_x + 2], ax
  2905.         jbe     @f
  2906.         mov     word [SLOT_BASE + edi + APPDATA.draw_bgr_x + 2], ax
  2907. @@:
  2908.         cmp     word [SLOT_BASE + edi + APPDATA.draw_bgr_y], dx
  2909.         jae     @f
  2910.         mov     word [SLOT_BASE + edi + APPDATA.draw_bgr_y], dx
  2911. @@:
  2912.         shr     edx, 16
  2913.         cmp     word [SLOT_BASE + edi + APPDATA.draw_bgr_y+2], dx
  2914.         jbe     @f
  2915.         mov     word [SLOT_BASE + edi + APPDATA.draw_bgr_y+2], dx
  2916. @@:
  2917.         jmp     .common
  2918. .set:
  2919.         mov     [SLOT_BASE + edi + APPDATA.draw_bgr_x], eax
  2920.         mov     [SLOT_BASE + edi + APPDATA.draw_bgr_y], edx
  2921. .common:
  2922.         or      [SLOT_BASE + edi + APPDATA.occurred_events], EVENT_BACKGROUND
  2923.         loop    set_bgr_event
  2924.         pop     edi ecx
  2925. ;--------- set event 5 stop -----------
  2926.         dec     [REDRAW_BACKGROUND]     ; got new update request?
  2927.         jnz     backgr
  2928.  
  2929.         xor     eax, eax
  2930.         mov     [draw_data + 32 + RECT.left], eax
  2931.         mov     [draw_data + 32 + RECT.top], eax
  2932.         mov     [draw_data + 32 + RECT.right], eax
  2933.         mov     [draw_data + 32 + RECT.bottom], eax
  2934. ;--------------------------------------
  2935. align 4
  2936. nobackgr:
  2937. ; system shutdown request
  2938.         cmp     [SYS_SHUTDOWN], byte 0
  2939.         je      noshutdown
  2940.  
  2941.         mov     edx, [shutdown_processes]
  2942.  
  2943.         cmp     [SYS_SHUTDOWN], dl
  2944.         jne     noshutdown
  2945.  
  2946.         lea     ecx, [edx-1]
  2947.         mov     edx, SLOT_BASE + sizeof.APPDATA ;OS_BASE+0x3040
  2948.         jecxz   no_mark_system_shutdown
  2949. ;--------------------------------------
  2950. align 4
  2951. markz:
  2952.         push    ecx edx
  2953.         cmp     [edx + APPDATA.state], TSTATE_FREE
  2954.         jz      .nokill
  2955.         cmp     [edx + APPDATA.process], sys_proc
  2956.         jz      .nokill
  2957.         call    request_terminate
  2958.         jmp     .common
  2959. .nokill:
  2960.         dec     byte [SYS_SHUTDOWN]
  2961.         xor     eax, eax
  2962. .common:
  2963.         pop     edx ecx
  2964.         test    eax, eax
  2965.         jz      @f
  2966.         mov     [edx + APPDATA.state], TSTATE_ZOMBIE
  2967. @@:
  2968.         add     edx, sizeof.APPDATA
  2969.         loop    markz
  2970.         call    wakeup_osloop
  2971. ;--------------------------------------
  2972. align 4
  2973. @@:
  2974. no_mark_system_shutdown:
  2975.         dec     byte [SYS_SHUTDOWN]
  2976.         je      system_shutdown
  2977. ;--------------------------------------
  2978. align 4
  2979. noshutdown:
  2980.         mov     eax, [thread_count]           ; termination
  2981.         mov     ebx, SLOT_BASE + sizeof.APPDATA + APPDATA.state
  2982.         mov     esi, 1
  2983. ;--------------------------------------
  2984. align 4
  2985. newct:
  2986.         mov     cl, [ebx]
  2987.         cmp     cl, TSTATE_ZOMBIE
  2988.         jz      .terminate
  2989.  
  2990.         cmp     cl, TSTATE_TERMINATING
  2991.         jnz     .noterminate
  2992. .terminate:
  2993.         pushad
  2994.         mov     ecx, eax
  2995.         shl     ecx, BSF sizeof.APPDATA
  2996.         add     ecx, SLOT_BASE
  2997.         call    restore_default_cursor_before_killing
  2998.         popad
  2999.  
  3000.         pushad
  3001.         call    terminate
  3002.         popad
  3003.         cmp     byte [SYS_SHUTDOWN], 0
  3004.         jz      .noterminate
  3005.         dec     byte [SYS_SHUTDOWN]
  3006.         je      system_shutdown
  3007.  
  3008. .noterminate:
  3009.         add     ebx, sizeof.APPDATA
  3010.         inc     esi
  3011.         dec     eax
  3012.         jnz     newct
  3013.         ret
  3014. ;-----------------------------------------------------------------------------
  3015. align 4
  3016. redrawscreen:
  3017. ; eax , if process window_data base is eax, do not set flag/limits
  3018.  
  3019.         pushad
  3020.         push    eax
  3021.  
  3022. ;;;         mov   ebx,2
  3023. ;;;         call  delay_hs
  3024.  
  3025.          ;mov   ecx,0               ; redraw flags for apps
  3026.         xor     ecx, ecx
  3027. ;--------------------------------------
  3028. align 4
  3029. newdw2:
  3030.         inc     ecx
  3031.         push    ecx
  3032.  
  3033.         mov     eax, ecx
  3034.         shl     eax, BSF sizeof.WDATA
  3035.         add     eax, window_data
  3036.  
  3037.         cmp     eax, [esp+4]
  3038.         je      not_this_task
  3039.                                    ; check if window in redraw area
  3040.         mov     edi, eax
  3041.  
  3042.         cmp     ecx, 1             ; limit for background
  3043.         jz      bgli
  3044.  
  3045.         mov     eax, [esp+4]        ;if upper in z-position - no redraw
  3046.         test    eax, eax
  3047.         jz      @f
  3048.         mov     al, [eax + WDATA.z_modif]
  3049.         cmp     [edi + WDATA.z_modif], al
  3050.         jg      ricino
  3051.       @@:
  3052.  
  3053.         mov     eax, [edi + WDATA.box.left]
  3054.         mov     ebx, [edi + WDATA.box.top]
  3055.  
  3056.         mov     ecx, [draw_limits.bottom] ; ecx = area y end     ebx = window y start
  3057.         cmp     ecx, ebx
  3058.         jb      ricino
  3059.  
  3060.         mov     ecx, [draw_limits.right] ; ecx = area x end     eax = window x start
  3061.         cmp     ecx, eax
  3062.         jb      ricino
  3063.  
  3064.         mov     eax, [edi + WDATA.box.left]
  3065.         mov     ebx, [edi + WDATA.box.top]
  3066.         mov     ecx, [edi + WDATA.box.width]
  3067.         mov     edx, [edi + WDATA.box.height]
  3068.         add     ecx, eax
  3069.         add     edx, ebx
  3070.  
  3071.         mov     eax, [draw_limits.top]  ; eax = area y start     edx = window y end
  3072.         cmp     edx, eax
  3073.         jb      ricino
  3074.  
  3075.         mov     eax, [draw_limits.left]  ; eax = area x start     ecx = window x end
  3076.         cmp     ecx, eax
  3077.         jb      ricino
  3078. ;--------------------------------------
  3079. align 4
  3080. bgli:
  3081.         cmp     dword[esp], 1
  3082.         jnz     .az
  3083.  
  3084.         cmp     [REDRAW_BACKGROUND], 0
  3085.         jz      .az
  3086.  
  3087.         mov     dl, 0
  3088.         lea     eax, [edi + draw_data - window_data]
  3089.         mov     ebx, [draw_limits.left]
  3090.         cmp     ebx, [eax + RECT.left]
  3091.         jae     @f
  3092.  
  3093.         mov     [eax+RECT.left], ebx
  3094.         mov     dl, 1
  3095. ;--------------------------------------
  3096. align 4
  3097. @@:
  3098.         mov     ebx, [draw_limits.top]
  3099.         cmp     ebx, [eax + RECT.top]
  3100.         jae     @f
  3101.  
  3102.         mov     [eax + RECT.top], ebx
  3103.         mov     dl, 1
  3104. ;--------------------------------------
  3105. align 4
  3106. @@:
  3107.         mov     ebx, [draw_limits.right]
  3108.         cmp     ebx, [eax + RECT.right]
  3109.         jbe     @f
  3110.  
  3111.         mov     [eax+RECT.right], ebx
  3112.         mov     dl, 1
  3113. ;--------------------------------------
  3114. align 4
  3115. @@:
  3116.         mov     ebx, [draw_limits.bottom]
  3117.         cmp     ebx, [eax + RECT.bottom]
  3118.         jbe     @f
  3119.  
  3120.         mov     [eax + RECT.bottom], ebx
  3121.         mov     dl, 1
  3122. ;--------------------------------------
  3123. align 4
  3124. @@:
  3125.         add     [REDRAW_BACKGROUND], dl
  3126.         call    wakeup_osloop
  3127.         jmp     newdw8
  3128. ;--------------------------------------
  3129. align 4
  3130. .az:
  3131.         mov     eax, edi
  3132.         add     eax, draw_data-window_data
  3133.  
  3134.         mov     ebx, [draw_limits.left]        ; set limits
  3135.         mov     [eax + RECT.left], ebx
  3136.         mov     ebx, [draw_limits.top]
  3137.         mov     [eax + RECT.top], ebx
  3138.         mov     ebx, [draw_limits.right]
  3139.         mov     [eax + RECT.right], ebx
  3140.         mov     ebx, [draw_limits.bottom]
  3141.         mov     [eax + RECT.bottom], ebx
  3142.  
  3143.         sub     eax, draw_data-window_data
  3144.  
  3145.         cmp     dword [esp], 1
  3146.         jne     nobgrd
  3147.         inc     [REDRAW_BACKGROUND]
  3148.         call    wakeup_osloop
  3149. ;--------------------------------------
  3150. align 4
  3151. newdw8:
  3152. nobgrd:
  3153. ;--------------------------------------
  3154.         push    eax  edi ebp
  3155.         mov     edi, [esp+12]
  3156.         cmp     edi, 1
  3157.         je      .found
  3158.  
  3159.         mov     eax, [draw_limits.left]
  3160.         mov     ebx, [draw_limits.top]
  3161.         mov     ecx, [draw_limits.right]
  3162.         sub     ecx, eax
  3163.         test    ecx, ecx
  3164.         jz      .not_found
  3165.  
  3166.         mov     edx, [draw_limits.bottom]
  3167.         sub     edx, ebx
  3168.         test    edx, edx
  3169.         jz      .not_found
  3170.  
  3171. ; eax - x, ebx - y
  3172. ; ecx - size x, edx - size y
  3173.         add     ebx, edx
  3174. ;--------------------------------------
  3175. align 4
  3176. .start_y:
  3177.         push    ecx
  3178. ;--------------------------------------
  3179. align 4
  3180. .start_x:
  3181.         add     eax, ecx
  3182.         mov     ebp, [d_width_calc_area + ebx*4]
  3183.         add     ebp, [_display.win_map]
  3184.         movzx   ebp, byte[eax+ebp] ; get value for current point
  3185.         cmp     ebp, edi
  3186.         jne     @f
  3187.  
  3188.         pop     ecx
  3189.         jmp     .found
  3190. ;--------------------------------------
  3191. align 4
  3192. @@:
  3193.         sub     eax, ecx
  3194.  
  3195.         dec     ecx
  3196.         jns     .start_x
  3197.  
  3198.         pop     ecx
  3199.         dec     ebx
  3200.         dec     edx
  3201.         jns     .start_y
  3202. ;--------------------------------------
  3203. align 4
  3204. .not_found:
  3205.         pop     ebp edi eax
  3206.         jmp     ricino
  3207. ;--------------------------------------
  3208. align 4
  3209. .found:
  3210.         pop     ebp edi eax
  3211.  
  3212.         mov     [eax + WDATA.fl_redraw], byte 1  ; mark as redraw
  3213. ;--------------------------------------
  3214. align 4
  3215. ricino:
  3216. not_this_task:
  3217.         pop     ecx
  3218.  
  3219.         cmp     ecx, [thread_count]
  3220.         jle     newdw2
  3221.  
  3222.         pop     eax
  3223.         popad
  3224.         ret
  3225. ;-----------------------------------------------------------------------------
  3226. align 4
  3227. calculatebackground:   ; background
  3228.         mov     edi, [_display.win_map]              ; set os to use all pixels
  3229.         mov     eax, 0x01010101
  3230.         mov     ecx, [_display.win_map_size]
  3231.         shr     ecx, 2
  3232.         rep stosd
  3233.         mov     byte[window_data + sizeof.WDATA + WDATA.z_modif], ZPOS_DESKTOP
  3234.         mov     [REDRAW_BACKGROUND], 0
  3235.         ret
  3236. ;-----------------------------------------------------------------------------
  3237. uglobal
  3238.   imax    dd 0x0
  3239. endg
  3240. ;-----------------------------------------------------------------------------
  3241. align 4
  3242. delay_ms:     ; delay in 1/1000 sec
  3243.         pushad
  3244.  
  3245.         cmp     [hpet_base], 0
  3246.         jz      .no_hpet
  3247.         mov     eax, esi
  3248.         mov     edx, 1_000_000 ; ms to ns
  3249.         mul     edx
  3250.         mov     ebx, edx
  3251.         mov     ecx, eax
  3252.  
  3253.         push    ecx
  3254.         call    get_clock_ns
  3255.         pop     ecx
  3256.         mov     edi, edx
  3257.         mov     esi, eax
  3258. .wait:
  3259.         push    ecx
  3260.         call    get_clock_ns
  3261.         pop     ecx
  3262.         sub     eax, esi
  3263.         sbb     edx, edi
  3264.         sub     eax, ecx
  3265.         sbb     edx, ebx
  3266.         jc      .wait
  3267.         jmp     .done
  3268.  
  3269. .no_hpet:
  3270.         mov     ecx, esi
  3271.         ; <CPU clock fix by Sergey Kuzmin aka Wildwest>
  3272.         imul    ecx, 33941
  3273.         shr     ecx, 9
  3274.         ; </CPU clock fix>
  3275.  
  3276.         in      al, 0x61
  3277.         and     al, 0x10
  3278.         mov     ah, al
  3279.         cld
  3280.  
  3281. .cnt1:
  3282.         in      al, 0x61
  3283.         and     al, 0x10
  3284.         cmp     al, ah
  3285.         jz      .cnt1
  3286.  
  3287.         mov     ah, al
  3288.         loop    .cnt1
  3289.  
  3290. .done:
  3291.         popad
  3292.         ret
  3293. ;-----------------------------------------------------------------------------
  3294. align 4
  3295. set_app_param:
  3296.         mov     edi, [current_slot]
  3297.         xchg    ebx, [edi + APPDATA.event_mask] ; set new event mask
  3298.         mov     [esp + SYSCALL_STACK.eax], ebx  ; return old mask value
  3299.         ret
  3300. ;-----------------------------------------------------------------------------
  3301.  
  3302. ; this is for syscall
  3303. proc delay_hs_unprotected
  3304.         call    unprotect_from_terminate
  3305.         call    delay_hs
  3306.         call    protect_from_terminate
  3307.         ret
  3308. endp
  3309.  
  3310. if 1
  3311. align 4
  3312. delay_hs:     ; delay in 1/100 secs
  3313. ; ebx = delay time
  3314.         pushad
  3315.         push    ebx
  3316.         xor     esi, esi
  3317.         mov     ecx, MANUAL_DESTROY
  3318.         call    create_event
  3319.         test    eax, eax
  3320.         jz      .done
  3321.  
  3322.         mov     ebx, edx
  3323.         mov     ecx, [esp]
  3324.         push    edx
  3325.         push    eax
  3326.         call    wait_event_timeout
  3327.         pop     eax
  3328.         pop     ebx
  3329.         call    destroy_event
  3330. .done:
  3331.         add     esp, 4
  3332.         popad
  3333.         ret
  3334.  
  3335. else
  3336.  
  3337. align 4
  3338. delay_hs:     ; delay in 1/100 secs
  3339. ; ebx = delay time
  3340.         push    ecx
  3341.         push    edx
  3342.  
  3343.         mov     edx, [timer_ticks]
  3344. ;--------------------------------------
  3345. align 4
  3346. .newtic:
  3347.         mov     ecx, [timer_ticks]
  3348.         sub     ecx, edx
  3349.         cmp     ecx, ebx
  3350.         jae     .zerodelay
  3351.  
  3352.         call    change_task
  3353.  
  3354.         jmp     .newtic
  3355. ;--------------------------------------
  3356. align 4
  3357. .zerodelay:
  3358.         pop     edx
  3359.         pop     ecx
  3360.         ret
  3361. end if
  3362.  
  3363. ;-----------------------------------------------------------------------------
  3364. align 16        ;very often call this subrutine
  3365. memmove:       ; memory move in bytes
  3366. ; eax = from
  3367. ; ebx = to
  3368. ; ecx = no of bytes
  3369.         test    ecx, ecx
  3370.         jle     .ret
  3371.  
  3372.         push    esi edi ecx
  3373.  
  3374.         mov     edi, ebx
  3375.         mov     esi, eax
  3376.  
  3377.         test    ecx, not 11b
  3378.         jz      @f
  3379.  
  3380.         push    ecx
  3381.         shr     ecx, 2
  3382.         rep movsd
  3383.         pop     ecx
  3384.         and     ecx, 11b
  3385.         jz      .finish
  3386. ;--------------------------------------
  3387. align 4
  3388. @@:
  3389.         rep movsb
  3390. ;--------------------------------------
  3391. align 4
  3392. .finish:
  3393.         pop     ecx edi esi
  3394. ;--------------------------------------
  3395. align 4
  3396. .ret:
  3397.         ret
  3398. ;-----------------------------------------------------------------------------
  3399.  
  3400. ; in: eax = port
  3401. ;     ebp = subfunction
  3402. ;          0 - set access
  3403. ;          1 - clear access
  3404. ; out: not return value
  3405. align 4
  3406. set_io_access_rights:
  3407.         push    edi eax
  3408.         mov     edi, tss._io_map_0
  3409.  
  3410.         test    ebp, ebp         ; enable access - ebp = 0
  3411.         jnz     .siar1
  3412.  
  3413.         btr     [edi], eax
  3414.         pop     eax edi
  3415.         ret
  3416. .siar1:
  3417.         bts     [edi], eax      ; disable access - ebp = 1
  3418.         pop     eax edi
  3419.         ret
  3420.  
  3421. align 4
  3422. ; @brief ReservePortArea and FreePortArea
  3423. ; @param edx number end arrea of ports (include last number of port)
  3424. ; @param ecx number start arrea of ports
  3425. ; @param ebx sub function 0 - reserve, 1 - free
  3426. ; @param eax 46 - number function
  3427. ; @returns  eax = 0 - succesful eax = 1 - error
  3428. syscall_reserveportarea:        ; ReservePortArea and FreePortArea
  3429.  
  3430.         call    r_f_port_area
  3431.         mov     [esp + SYSCALL_STACK.eax], eax
  3432.         ret
  3433.  
  3434. ;reserve/free group of ports
  3435. ;  * eax = 46 - number function
  3436. ;  * ebx = 0 - reserve, 1 - free
  3437. ;  * ecx = number start arrea of ports
  3438. ;  * edx = number end arrea of ports (include last number of port)
  3439. ;Return value:
  3440. ;  * eax = 0 - succesful
  3441. ;  * eax = 1 - error
  3442. ;  * The system has reserve this ports:
  3443. ;    0..0x2d, 0x30..0x4d, 0x50..0xdf, 0xe5..0xff (include last number of port).
  3444. ;destroys eax,ebx, ebp
  3445. r_f_port_area:
  3446.  
  3447.         test    ebx, ebx
  3448.         jnz     .free_port_area
  3449.  
  3450.         cmp     ecx, edx      ; beginning > end ?
  3451.         ja      .rpal1
  3452.         cmp     edx, 65536    ;test ebx, not 0xffff
  3453.         jae     .rpal1
  3454.         mov     eax, [RESERVED_PORTS]
  3455.         test    eax, eax      ; no reserved areas ?
  3456.         je      .rpal2
  3457.         cmp     eax, 255      ; max reserved
  3458.         jae     .rpal1
  3459.  .rpal3:
  3460.         mov     ebx, eax
  3461.         shl     ebx, 4   ;16 byte is sizeof item in RESERVED_PORTS table
  3462.         add     ebx, RESERVED_PORTS
  3463.         cmp     ecx, [ebx+8]
  3464.         ja      .rpal4
  3465.         cmp     edx, [ebx+4]
  3466.         jae     .rpal1
  3467.  .rpal4:
  3468.         dec     eax
  3469.         jnz     .rpal3
  3470.         jmp     .rpal2
  3471.    .rpal1:
  3472.         xor     eax, eax
  3473.         inc     eax
  3474.         ret
  3475.    .rpal2:
  3476.      ; enable port access at port IO map
  3477.         pushad                        ; start enable io map
  3478.         mov     eax, ecx
  3479.         xor     ebp, ebp               ; enable - eax = port
  3480.         cli
  3481. .new_port_access:
  3482.         call    set_io_access_rights
  3483.  
  3484.         inc     eax
  3485.         cmp     eax, edx
  3486.         jbe     .new_port_access
  3487. ;no_unmask_io:
  3488.         sti
  3489.         popad                         ; end enable io map
  3490.  
  3491.         mov     eax, [RESERVED_PORTS]
  3492.         inc     eax
  3493.         mov     [RESERVED_PORTS], eax
  3494.         shl     eax, 4
  3495.         add     eax, RESERVED_PORTS
  3496.         mov     ebx, [current_slot]
  3497.         mov     ebx, [ebx + APPDATA.tid]
  3498.         mov     [eax], ebx   ; tid
  3499.         mov     [eax+4], ecx ;start port
  3500.         mov     [eax+8], edx ;finish port
  3501.  
  3502.         xor     eax, eax
  3503.         ret
  3504.  
  3505. .free_port_area:
  3506.  
  3507.         mov     eax, [RESERVED_PORTS]; no reserved areas ?
  3508.         test    eax, eax
  3509.         jz      .frpal2
  3510.         mov     ebx, [current_slot]
  3511.         mov     ebx, [ebx + APPDATA.tid]
  3512.    .frpal3:
  3513.         mov     edi, eax
  3514.         shl     edi, 4
  3515.         add     edi, RESERVED_PORTS
  3516.         cmp     ebx, [edi]
  3517.         jne     .frpal4
  3518.         cmp     ecx, [edi+4]
  3519.         jne     .frpal4
  3520.         cmp     edx, [edi+8]
  3521.         jne     .frpal4
  3522.         jmp     .frpal1
  3523.    .frpal4:
  3524.         dec     eax
  3525.         jnz     .frpal3
  3526.    .frpal2:
  3527.         inc     eax
  3528.         ret
  3529.    .frpal1:
  3530.         push    ecx
  3531.         mov     ecx, 256
  3532.         sub     ecx, eax
  3533.         shl     ecx, 4
  3534.         mov     esi, edi
  3535.         add     esi, 16
  3536.         cld
  3537.         rep movsb
  3538.  
  3539.         dec     dword [RESERVED_PORTS]
  3540. ;disable port access at port IO map
  3541.                         ; start disable io map
  3542.         pop     eax     ;start port
  3543.         ;cmp     edx, 65536
  3544.         ;jge     no_mask_io
  3545.  
  3546.         xor     ebp, ebp
  3547.         inc     ebp
  3548. .new_port_access_disable:           ; disable - eax = port
  3549.         call    set_io_access_rights
  3550.  
  3551.         inc     eax
  3552.         cmp     eax, edx
  3553.         jbe     .new_port_access_disable
  3554. ;no_mask_io:                         ; end disable io map
  3555.         xor     eax, eax
  3556.         ret
  3557. ;-----------------------------------------------------------------------------
  3558. align 4
  3559. drawbackground:
  3560.         cmp     [BgrDrawMode], dword 1
  3561.         jne     .bgrstr
  3562.         call    vesa20_drawbackground_tiled
  3563. ;        call    [draw_pointer]
  3564.         call    __sys_draw_pointer
  3565.         ret
  3566. ;--------------------------------------
  3567. align 4
  3568. .bgrstr:
  3569.         call    vesa20_drawbackground_stretch
  3570. ;        call    [draw_pointer]
  3571.         call    __sys_draw_pointer
  3572.         ret
  3573. ;-----------------------------------------------------------------------------
  3574. align 4
  3575. syscall_putimage:                       ; PutImage
  3576. ; add check pointer
  3577.         push    ecx
  3578.         mov     ax, cx
  3579.         shr     ecx, 16
  3580.         imul    eax, ecx
  3581.         lea     eax, [eax*3]
  3582.         stdcall is_region_userspace, ebx, eax
  3583.         pop     ecx
  3584.         jnz     sys_putimage.exit
  3585.  
  3586. sys_putimage:
  3587.         test    ecx, 0x80008000
  3588.         jnz     .exit
  3589.         test    ecx, 0x0000FFFF
  3590.         jz      .exit
  3591.         test    ecx, 0xFFFF0000
  3592.         jnz     @f
  3593. ;--------------------------------------
  3594. align 4
  3595. .exit:
  3596.         ret
  3597. ;--------------------------------------
  3598. align 4
  3599. @@:
  3600.         mov     edi, [current_slot]
  3601.         add     dx, word[edi + APPDATA.wnd_clientbox.top]
  3602.         rol     edx, 16
  3603.         add     dx, word[edi + APPDATA.wnd_clientbox.left]
  3604.         rol     edx, 16
  3605. ;--------------------------------------
  3606. align 4
  3607. .forced:
  3608.         push    ebp esi 0
  3609.         mov     ebp, putimage_get24bpp
  3610.         mov     esi, putimage_init24bpp
  3611. ;--------------------------------------
  3612. align 4
  3613. sys_putimage_bpp:
  3614.         call    vesa20_putimage
  3615.         pop     ebp esi ebp
  3616.         ret
  3617. ;        jmp     [draw_pointer]
  3618. ;-----------------------------------------------------------------------------
  3619. align 4
  3620. sys_putimage_palette:
  3621. ; ebx = pointer to image
  3622. ; ecx = [xsize]*65536 + [ysize]
  3623. ; edx = [xstart]*65536 + [ystart]
  3624. ; esi = number of bits per pixel, must be 8, 24 or 32
  3625. ; edi = pointer to palette
  3626. ; ebp = row delta
  3627. ; check pointer
  3628.         push    ecx esi
  3629.         mov     ax, cx
  3630.         shr     ecx, 16
  3631.         imul    eax, ecx
  3632. ;        imul    eax, esi ; eax*count bit in 1 pixel
  3633. ;        shr     eax, 3
  3634.         stdcall is_region_userspace, ebx, eax
  3635.         pop     esi ecx
  3636.         jnz     sys_putimage.exit
  3637.  
  3638.         mov     eax, [current_slot]
  3639.         add     dx, word [eax + APPDATA.wnd_clientbox.top]
  3640.         rol     edx, 16
  3641.         add     dx, word [eax + APPDATA.wnd_clientbox.left]
  3642.         rol     edx, 16
  3643. ;--------------------------------------
  3644. align 4
  3645. .forced:
  3646.         cmp     esi, 1
  3647.         jnz     @f
  3648.         push    edi
  3649.         mov     eax, [edi+4]
  3650.         sub     eax, [edi]
  3651.         push    eax
  3652.         push    dword [edi]
  3653.         push    0ffffff80h
  3654.         mov     edi, esp
  3655.         call    put_mono_image
  3656.         add     esp, 12
  3657.         pop     edi
  3658.         ret
  3659. ;--------------------------------------
  3660. align 4
  3661. @@:
  3662.         cmp     esi, 2
  3663.         jnz     @f
  3664.         push    edi
  3665.         push    0ffffff80h
  3666.         mov     edi, esp
  3667.         call    put_2bit_image
  3668.         pop     eax
  3669.         pop     edi
  3670.         ret
  3671. ;--------------------------------------
  3672. align 4
  3673. @@:
  3674.         cmp     esi, 4
  3675.         jnz     @f
  3676.         push    edi
  3677.         push    0ffffff80h
  3678.         mov     edi, esp
  3679.         call    put_4bit_image
  3680.         pop     eax
  3681.         pop     edi
  3682.         ret
  3683. ;--------------------------------------
  3684. align 4
  3685. @@:
  3686.         push    ebp esi ebp
  3687.         cmp     esi, 8
  3688.         jnz     @f
  3689.         mov     ebp, putimage_get8bpp
  3690.         mov     esi, putimage_init8bpp
  3691.         jmp     sys_putimage_bpp
  3692. ;--------------------------------------
  3693. align 4
  3694. @@:
  3695.         cmp     esi, 9
  3696.         jnz     @f
  3697.         mov     ebp, putimage_get9bpp
  3698.         mov     esi, putimage_init9bpp
  3699.         jmp     sys_putimage_bpp
  3700. ;--------------------------------------
  3701. align 4
  3702. @@:
  3703.         cmp     esi, 15
  3704.         jnz     @f
  3705.         mov     ebp, putimage_get15bpp
  3706.         mov     esi, putimage_init15bpp
  3707.         jmp     sys_putimage_bpp
  3708. ;--------------------------------------
  3709. align 4
  3710. @@:
  3711.         cmp     esi, 16
  3712.         jnz     @f
  3713.         mov     ebp, putimage_get16bpp
  3714.         mov     esi, putimage_init16bpp
  3715.         jmp     sys_putimage_bpp
  3716. ;--------------------------------------
  3717. align 4
  3718. @@:
  3719.         cmp     esi, 24
  3720.         jnz     @f
  3721.         mov     ebp, putimage_get24bpp
  3722.         mov     esi, putimage_init24bpp
  3723.         jmp     sys_putimage_bpp
  3724. ;--------------------------------------
  3725. align 4
  3726. @@:
  3727.         cmp     esi, 32
  3728.         jnz     @f
  3729.         mov     ebp, putimage_get32bpp
  3730.         mov     esi, putimage_init32bpp
  3731.         jmp     sys_putimage_bpp
  3732. ;--------------------------------------
  3733. align 4
  3734. @@:
  3735.         pop     ebp esi ebp
  3736.         ret
  3737. ;-----------------------------------------------------------------------------
  3738. align 4
  3739. put_mono_image:
  3740.         push    ebp esi ebp
  3741.         mov     ebp, putimage_get1bpp
  3742.         mov     esi, putimage_init1bpp
  3743.         jmp     sys_putimage_bpp
  3744. ;-----------------------------------------------------------------------------
  3745. align 4
  3746. put_2bit_image:
  3747.         push    ebp esi ebp
  3748.         mov     ebp, putimage_get2bpp
  3749.         mov     esi, putimage_init2bpp
  3750.         jmp     sys_putimage_bpp
  3751. ;-----------------------------------------------------------------------------
  3752. align 4
  3753. put_4bit_image:
  3754.         push    ebp esi ebp
  3755.         mov     ebp, putimage_get4bpp
  3756.         mov     esi, putimage_init4bpp
  3757.         jmp     sys_putimage_bpp
  3758. ;-----------------------------------------------------------------------------
  3759. align 4
  3760. putimage_init24bpp:
  3761.         lea     eax, [eax*3]
  3762. putimage_init8bpp:
  3763. putimage_init9bpp:
  3764.         ret
  3765. ;-----------------------------------------------------------------------------
  3766. align 16
  3767. putimage_get24bpp:
  3768.         movzx   eax, byte [esi+2]
  3769.         shl     eax, 16
  3770.         mov     ax, [esi]
  3771.         add     esi, 3
  3772.         ret     4
  3773. ;-----------------------------------------------------------------------------
  3774. align 16
  3775. putimage_get8bpp:
  3776.         movzx   eax, byte [esi]
  3777.         push    edx
  3778.         mov     edx, [esp+8]
  3779.         mov     eax, [edx + eax*4]
  3780.         pop     edx
  3781.         inc     esi
  3782.         ret     4
  3783. ;-----------------------------------------------------------------------------
  3784. align 16
  3785. putimage_get9bpp:
  3786.         lodsb
  3787.         mov     ah, al
  3788.         shl     eax, 8
  3789.         mov     al, ah
  3790.         ret     4
  3791. ;-----------------------------------------------------------------------------
  3792. align 4
  3793. putimage_init1bpp:
  3794.         add     eax, ecx
  3795.         push    ecx
  3796.         add     eax, 7
  3797.         add     ecx, 7
  3798.         shr     eax, 3
  3799.         shr     ecx, 3
  3800.         sub     eax, ecx
  3801.         pop     ecx
  3802.         ret
  3803. ;-----------------------------------------------------------------------------
  3804. align 16
  3805. putimage_get1bpp:
  3806.         push    edx
  3807.         mov     edx, [esp+8]
  3808.         mov     al, [edx]
  3809.         add     al, al
  3810.         jnz     @f
  3811.         lodsb
  3812.         adc     al, al
  3813. @@:
  3814.         mov     [edx], al
  3815.         sbb     eax, eax
  3816.         and     eax, [edx+8]
  3817.         add     eax, [edx+4]
  3818.         pop     edx
  3819.         ret     4
  3820. ;-----------------------------------------------------------------------------
  3821. align 4
  3822. putimage_init2bpp:
  3823.         add     eax, ecx
  3824.         push    ecx
  3825.         add     ecx, 3
  3826.         add     eax, 3
  3827.         shr     ecx, 2
  3828.         shr     eax, 2
  3829.         sub     eax, ecx
  3830.         pop     ecx
  3831.         ret
  3832. ;-----------------------------------------------------------------------------
  3833. align 16
  3834. putimage_get2bpp:
  3835.         push    edx
  3836.         mov     edx, [esp+8]
  3837.         mov     al, [edx]
  3838.         mov     ah, al
  3839.         shr     al, 6
  3840.         shl     ah, 2
  3841.         jnz     .nonewbyte
  3842.         lodsb
  3843.         mov     ah, al
  3844.         shr     al, 6
  3845.         shl     ah, 2
  3846.         add     ah, 1
  3847. .nonewbyte:
  3848.         mov     [edx], ah
  3849.         mov     edx, [edx+4]
  3850.         movzx   eax, al
  3851.         mov     eax, [edx + eax*4]
  3852.         pop     edx
  3853.         ret     4
  3854. ;-----------------------------------------------------------------------------
  3855. align 4
  3856. putimage_init4bpp:
  3857.         add     eax, ecx
  3858.         push    ecx
  3859.         add     ecx, 1
  3860.         inc     eax      ;add   eax, 1
  3861.         shr     ecx, 1
  3862.         shr     eax, 1
  3863.         sub     eax, ecx
  3864.         pop     ecx
  3865.         ret
  3866. ;-----------------------------------------------------------------------------
  3867. align 16
  3868. putimage_get4bpp:
  3869.         push    edx
  3870.         mov     edx, [esp+8]
  3871.         add     byte [edx], 80h
  3872.         jc      @f
  3873.         movzx   eax, byte [edx+1]
  3874.         mov     edx, [edx+4]
  3875.         and     eax, 0x0F
  3876.         mov     eax, [edx + eax*4]
  3877.         pop     edx
  3878.         ret     4
  3879. @@:
  3880.         movzx   eax, byte [esi]
  3881.         add     esi, 1
  3882.         mov     [edx+1], al
  3883.         shr     eax, 4
  3884.         mov     edx, [edx+4]
  3885.         mov     eax, [edx + eax*4]
  3886.         pop     edx
  3887.         ret     4
  3888. ;-----------------------------------------------------------------------------
  3889. align 4
  3890. putimage_init32bpp:
  3891.         shl     eax, 2
  3892.         ret
  3893. ;-----------------------------------------------------------------------------
  3894. align 16
  3895. putimage_get32bpp:
  3896.         lodsd
  3897.         ret     4
  3898. ;-----------------------------------------------------------------------------
  3899. align 4
  3900. putimage_init15bpp:
  3901. putimage_init16bpp:
  3902.         add     eax, eax
  3903.         ret
  3904. ;-----------------------------------------------------------------------------
  3905. align 16
  3906. putimage_get15bpp:
  3907. ; 0RRRRRGGGGGBBBBB -> 00000000RRRRR000GGGGG000BBBBB000
  3908.         push    ecx edx
  3909.         movzx   eax, word [esi]
  3910.         add     esi, 2
  3911.         mov     ecx, eax
  3912.         mov     edx, eax
  3913.         and     eax, 0x1F
  3914.         and     ecx, 0x1F shl 5
  3915.         and     edx, 0x1F shl 10
  3916.         shl     eax, 3
  3917.         shl     ecx, 6
  3918.         shl     edx, 9
  3919.         or      eax, ecx
  3920.         or      eax, edx
  3921.         pop     edx ecx
  3922.         ret     4
  3923. ;-----------------------------------------------------------------------------
  3924. align 16
  3925. putimage_get16bpp:
  3926. ; RRRRRGGGGGGBBBBB -> 00000000RRRRR000GGGGGG00BBBBB000
  3927.         push    ecx edx
  3928.         movzx   eax, word [esi]
  3929.         add     esi, 2
  3930.         mov     ecx, eax
  3931.         mov     edx, eax
  3932.         and     eax, 0x1F
  3933.         and     ecx, 0x3F shl 5
  3934.         and     edx, 0x1F shl 11
  3935.         shl     eax, 3
  3936.         shl     ecx, 5
  3937.         shl     edx, 8
  3938.         or      eax, ecx
  3939.         or      eax, edx
  3940.         pop     edx ecx
  3941.         ret     4
  3942. ;-----------------------------------------------------------------------------
  3943. ;align 4
  3944. ; eax x beginning
  3945. ; ebx y beginning
  3946. ; ecx x end
  3947.         ; edx y end
  3948. ; edi color
  3949. ;__sys_drawbar:
  3950. ;        mov     esi, [current_slot]
  3951. ;        add     eax, [esi+APPDATA.wnd_clientbox.left]
  3952. ;        add     ecx, [esi+APPDATA.wnd_clientbox.left]
  3953. ;        add     ebx, [esi+APPDATA.wnd_clientbox.top]
  3954. ;        add     edx, [esi+APPDATA.wnd_clientbox.top]
  3955. ;--------------------------------------
  3956. ;align 4
  3957. ;.forced:
  3958. ;        call    vesa20_drawbar
  3959. ;        call    [draw_pointer]
  3960. ;        ret
  3961. ;-----------------------------------------------------------------------------
  3962. if used _rdtsc
  3963. _rdtsc:
  3964.         bt      [cpu_caps], CAPS_TSC
  3965.         jnc     .ret_rdtsc
  3966.         rdtsc
  3967.         ret
  3968.    .ret_rdtsc:
  3969.         mov     edx, 0xffffffff
  3970.         mov     eax, 0xffffffff
  3971.         ret
  3972. end if
  3973.  
  3974. sys_msg_board_str:
  3975.  
  3976.         pushad
  3977.    @@:
  3978.         cmp     [esi], byte 0
  3979.         je      @f
  3980.         mov     ebx, 1
  3981.         movzx   ecx, byte [esi]
  3982.         call    sys_msg_board
  3983.         inc     esi
  3984.         jmp     @b
  3985.    @@:
  3986.         popad
  3987.         ret
  3988.  
  3989. sys_msg_board_byte:
  3990. ; in: al = byte to display
  3991. ; out: nothing
  3992. ; destroys: nothing
  3993.         pushad
  3994.         mov     ecx, 2
  3995.         shl     eax, 24
  3996.         jmp     @f
  3997.  
  3998. sys_msg_board_word:
  3999. ; in: ax = word to display
  4000. ; out: nothing
  4001. ; destroys: nothing
  4002.         pushad
  4003.         mov     ecx, 4
  4004.         shl     eax, 16
  4005.         jmp     @f
  4006.  
  4007. sys_msg_board_dword:
  4008. ; in: eax = dword to display
  4009. ; out: nothing
  4010. ; destroys: nothing
  4011.         pushad
  4012.         mov     ecx, 8
  4013. @@:
  4014.         push    ecx
  4015.         rol     eax, 4
  4016.         push    eax
  4017.         and     al, 0xF
  4018.         cmp     al, 10
  4019.         sbb     al, 69h
  4020.         das
  4021.         mov     cl, al
  4022.         xor     ebx, ebx
  4023.         inc     ebx
  4024.         call    sys_msg_board
  4025.         pop     eax
  4026.         pop     ecx
  4027.         loop    @b
  4028.         popad
  4029.         ret
  4030.  
  4031. msg_board_data_size = 65536 ; Must be power of two
  4032.  
  4033. uglobal
  4034. msg_board_data  rb  msg_board_data_size
  4035. msg_board_count dd  ?
  4036. endg
  4037.  
  4038. iglobal
  4039. msg_board_pos   dd  42*6*65536+10 ; for printing debug output on the screen
  4040. endg
  4041.  
  4042. sys_msg_board:
  4043. ; ebx=1 -> write, cl = byte to write
  4044. ; ebx=2 -> read, ecx=0 -> no data, ecx=1 -> data in al
  4045.         push    eax ebx
  4046.         mov     eax, ebx
  4047.         mov     ebx, ecx
  4048.         mov     ecx, [msg_board_count]
  4049.         cmp     eax, 1
  4050.         jne     .read
  4051.  
  4052. if defined debug_com_base
  4053.         push    dx ax
  4054. @@: ; wait for empty transmit register
  4055.         mov     dx, debug_com_base+5
  4056.         in      al, dx
  4057.         test    al, 1 shl 5
  4058.         jz      @r
  4059.         mov     dx, debug_com_base      ; Output the byte
  4060.         mov     al, bl
  4061.         out     dx, al
  4062.         pop     ax dx
  4063. end if
  4064.  
  4065.         mov     [msg_board_data+ecx], bl
  4066.         cmp     byte [debug_direct_print], 1
  4067.         jnz     .end
  4068.         pusha
  4069.         lea     edx, [msg_board_data+ecx]
  4070.         mov     ecx, 0x40FFFFFF
  4071.         mov     ebx, [msg_board_pos]
  4072.         mov     edi, 1
  4073.         mov     esi, edi ;1
  4074.         call    dtext
  4075.         popa
  4076.         add     word [msg_board_pos+2], 6
  4077.         cmp     word [msg_board_pos+2], 105*6
  4078.         jnc     @f
  4079.         cmp     bl, 10
  4080.         jnz     .end
  4081. @@:
  4082.         mov     word [msg_board_pos+2], 42*6
  4083.         add     word [msg_board_pos], 10
  4084.         mov     eax, [_display.height]
  4085.         sub     eax, 10
  4086.         cmp     ax, word [msg_board_pos]
  4087.         jnc     @f
  4088.         mov     word [msg_board_pos], 10
  4089. @@:
  4090.         pusha
  4091.         mov     eax, [msg_board_pos]
  4092.         movzx   ebx, ax
  4093.         shr     eax, 16
  4094.         mov     edx, 105*6
  4095.         xor     ecx, ecx
  4096.         mov     edi, 1
  4097.         mov     esi, 9
  4098. @@:
  4099.         call    hline
  4100.         inc     ebx
  4101.         dec     esi
  4102.         jnz     @b
  4103.         popa
  4104. .end:
  4105.         inc     ecx
  4106.         and     ecx, msg_board_data_size - 1
  4107.         mov     [msg_board_count], ecx
  4108. .ret:
  4109.         pop     ebx eax
  4110.         ret
  4111.  
  4112. .read:
  4113.         cmp     eax, 2
  4114.         jne     .ret
  4115.         add     esp, 8  ; returning data in ebx and eax, so no need to restore them
  4116.         test    ecx, ecx
  4117.         jnz     @f
  4118.         mov     [esp + SYSCALL_STACK.eax], ecx
  4119.         mov     [esp + SYSCALL_STACK.ebx], ecx
  4120.         ret
  4121. @@:
  4122.         mov     eax, msg_board_data+1
  4123.         mov     ebx, msg_board_data
  4124.         movzx   edx, byte [ebx]
  4125.         call    memmove
  4126.         dec     [msg_board_count]
  4127.         mov     [esp + SYSCALL_STACK.eax], edx
  4128.         mov     [esp + SYSCALL_STACK.ebx], 1
  4129.         ret
  4130.  
  4131. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4132. ;; 61 sys function.                                                ;;
  4133. ;; in eax=61,ebx in [1..3]                                         ;;
  4134. ;; out eax                                                         ;;
  4135. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  4136. iglobal
  4137. align 4
  4138. f61call:
  4139.            dd sys_gs.1   ; resolution
  4140.            dd sys_gs.2   ; bits per pixel
  4141.            dd sys_gs.3   ; bytes per scanline
  4142. endg
  4143.  
  4144.  
  4145. align 4
  4146.  
  4147. sys_gs:                         ; direct screen access
  4148.         dec     ebx
  4149.         cmp     ebx, 2
  4150.         ja      .not_support
  4151.         jmp     dword [f61call + ebx*4]
  4152. .not_support:
  4153.         or      [esp + SYSCALL_STACK.eax], dword -1
  4154.         ret
  4155.  
  4156.  
  4157. .1:                             ; resolution
  4158.         mov     eax, [_display.width]
  4159.         shl     eax, 16
  4160.         mov     ax, word [_display.height]
  4161.         mov     [esp + SYSCALL_STACK.eax], eax
  4162.         ret
  4163. .2:                             ; bits per pixel
  4164.         mov     eax, [_display.bits_per_pixel]
  4165.         mov     [esp + SYSCALL_STACK.eax], eax
  4166.         ret
  4167. .3:                             ; bytes per scanline
  4168.         mov     eax, [_display.lfb_pitch]
  4169.         mov     [esp + SYSCALL_STACK.eax], eax
  4170.         ret
  4171.  
  4172. align 4
  4173. syscall_getscreensize:                  ; GetScreenSize
  4174.         mov     ax, word [_display.width]
  4175.         dec     ax
  4176.         shl     eax, 16
  4177.         mov     ax, word [_display.height]
  4178.         dec     ax
  4179.         mov     [esp + SYSCALL_STACK.eax], eax
  4180.         ret
  4181. ;-----------------------------------------------------------------------------
  4182. align 4
  4183. syscall_cdaudio:
  4184. ; ECX - position of CD/DVD-drive
  4185. ; from 0=Primary Master to 3=Secondary Slave for first IDE contr.
  4186. ; from 4=Primary Master to 7=Secondary Slave for second IDE contr.
  4187. ; from 8=Primary Master to 11=Secondary Slave for third IDE contr.
  4188.         cmp     ecx, 11
  4189.         ja      .exit
  4190.  
  4191.         mov     eax, ecx
  4192.         shr     eax, 2
  4193.         lea     eax, [eax*5]
  4194.         mov     al, [eax + DRIVE_DATA + 1]
  4195.  
  4196.         push    ecx ebx
  4197.         mov     ebx, ecx
  4198.         and     ebx, 11b
  4199.         shl     ebx, 1
  4200.         mov     cl, 6
  4201.         sub     cl, bl
  4202.         shr     al, cl
  4203.         test    al, 2 ; it's not an ATAPI device
  4204.         pop     ebx ecx
  4205.  
  4206.         jz      .exit
  4207.  
  4208.         cmp     ebx, 4
  4209.         je      .eject
  4210.  
  4211.         cmp     ebx, 5
  4212.         je      .load
  4213. ;--------------------------------------
  4214. .exit:
  4215.         ret
  4216. ;--------------------------------------
  4217. .load:
  4218.         call    .reserve
  4219.         call    LoadMedium
  4220.         jmp     .free
  4221. ;--------------------------------------
  4222. .eject:
  4223.         call    .reserve
  4224.         call    clear_CD_cache
  4225.         call    allow_medium_removal
  4226.         call    EjectMedium
  4227.         jmp     .free
  4228. ;--------------------------------------
  4229. .reserve:
  4230.         call    reserve_cd
  4231.  
  4232.         mov     ebx, ecx
  4233.         inc     ebx
  4234.         mov     [cdpos], ebx
  4235.  
  4236.         mov     eax, ecx
  4237.         shr     eax, 1
  4238.         and     eax, 1
  4239.         inc     eax
  4240.         mov     [ChannelNumber], al
  4241.         mov     eax, ecx
  4242.         and     eax, 1
  4243.         mov     [DiskNumber], al
  4244.         call    reserve_cd_channel
  4245.         ret
  4246. ;--------------------------------------
  4247. .free:
  4248.         call    free_cd_channel
  4249.         and     [cd_status], 0
  4250.         ret
  4251. ;-----------------------------------------------------------------------------
  4252. align 4
  4253. syscall_getpixel_WinMap:                       ; GetPixel WinMap
  4254.         cmp     ebx, [_display.width]
  4255.         jb      @f
  4256.         cmp     ecx, [_display.height]
  4257.         jb      @f
  4258.         xor     eax, eax
  4259.         jmp     .store
  4260. ;--------------------------------------
  4261. align 4
  4262. @@:
  4263.         mov     eax, [d_width_calc_area + ecx*4]
  4264.         add     eax, [_display.win_map]
  4265.         movzx   eax, byte[eax+ebx]        ; get value for current point
  4266. ;--------------------------------------
  4267. align 4
  4268. .store:
  4269.         mov     [esp + SYSCALL_STACK.eax], eax
  4270.         ret
  4271. ;-----------------------------------------------------------------------------
  4272. align 4
  4273. syscall_getpixel:                       ; GetPixel
  4274.         mov     ecx, [_display.width]
  4275.         xor     edx, edx
  4276.         mov     eax, ebx
  4277.         div     ecx
  4278.         mov     ebx, edx
  4279.         xchg    eax, ebx
  4280.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4281.         call    dword [GETPIXEL]; eax - x, ebx - y
  4282.         mov     [esp + SYSCALL_STACK.eax], ecx
  4283.         ret
  4284. ;-----------------------------------------------------------------------------
  4285. align 4
  4286. syscall_getarea:
  4287. ;eax = 36
  4288. ;ebx = pointer to bufer for img BBGGRRBBGGRR...
  4289. ;ecx = [size x]*65536 + [size y]
  4290. ;edx = [start x]*65536 + [start y]
  4291.         pushad
  4292.         mov     edi, ebx
  4293.         mov     eax, edx
  4294.         shr     eax, 16
  4295.         mov     ebx, edx
  4296.         and     ebx, 0xffff
  4297.         dec     eax
  4298.         dec     ebx
  4299.      ; eax - x, ebx - y
  4300.         mov     edx, ecx
  4301.  
  4302.         shr     ecx, 16
  4303.         and     edx, 0xffff
  4304.         mov     esi, ecx
  4305.      ; ecx - size x, edx - size y
  4306.  
  4307.         mov     ebp, edx
  4308.         lea     ebp, [ebp*3]
  4309.         imul    ebp, esi
  4310.         stdcall is_region_userspace, edi, ebp
  4311.         jnz     .exit
  4312.  
  4313.         mov     ebp, edx
  4314.         dec     ebp
  4315.         lea     ebp, [ebp*3]
  4316.  
  4317.         imul    ebp, esi
  4318.  
  4319.         mov     esi, ecx
  4320.         dec     esi
  4321.         lea     esi, [esi*3]
  4322.  
  4323.         add     ebp, esi
  4324.         add     ebp, edi
  4325.  
  4326.         add     ebx, edx
  4327. ;--------------------------------------
  4328. align 4
  4329. .start_y:
  4330.         push    ecx edx
  4331. ;--------------------------------------
  4332. align 4
  4333. .start_x:
  4334.         push    eax ebx ecx
  4335.         add     eax, ecx
  4336.  
  4337.         and     ecx, 0xFBFFFFFF  ;negate 0x04000000 use mouseunder area
  4338.         call    dword [GETPIXEL]; eax - x, ebx - y
  4339.  
  4340.         mov     [ebp], cx
  4341.         shr     ecx, 16
  4342.         mov     [ebp+2], cl
  4343.  
  4344.         pop     ecx ebx eax
  4345.         sub     ebp, 3
  4346.         dec     ecx
  4347.         jnz     .start_x
  4348.         pop     edx ecx
  4349.         dec     ebx
  4350.         dec     edx
  4351.         jnz     .start_y
  4352.        
  4353. .exit:
  4354.         popad
  4355.         ret
  4356. ;-----------------------------------------------------------------------------
  4357.  
  4358. align 4
  4359. syscall_threads:                        ; CreateThreads
  4360. ;
  4361. ;   ecx=thread entry point
  4362. ;   edx=thread stack pointer
  4363. ;
  4364. ; on return : eax = pid
  4365.         xor     ebx, ebx
  4366.         call    new_sys_threads
  4367.  
  4368.         mov     [esp + SYSCALL_STACK.eax], eax
  4369.         ret
  4370.  
  4371. ;------------------------------------------------------------------------------
  4372. align 4
  4373. calculate_fast_getting_offset_for_WinMapAddress:
  4374. ; calculate data area for fast getting offset to _WinMapAddress
  4375.         xor     eax, eax
  4376.         mov     ecx, [_display.height]
  4377.         mov     edi, d_width_calc_area
  4378.         cld
  4379. @@:
  4380.         stosd
  4381.         add     eax, [_display.width]
  4382.         dec     ecx
  4383.         jnz     @r
  4384.         ret
  4385. ;------------------------------------------------------------------------------
  4386. align 4
  4387. calculate_fast_getting_offset_for_LFB:
  4388. ; calculate data area for fast getting offset to LFB
  4389.         xor     eax, eax
  4390.         mov     ecx, [_display.height]
  4391.         mov     edi, BPSLine_calc_area
  4392.         cld
  4393. @@:
  4394.         stosd
  4395.         add     eax, [_display.lfb_pitch]
  4396.         dec     ecx
  4397.         jnz     @r
  4398.         ret
  4399. ;------------------------------------------------------------------------------
  4400. align 4
  4401. set_screen:
  4402. ; in:
  4403. ; eax - new Screen_Max_X
  4404. ; ecx - new BytesPerScanLine
  4405. ; edx - new Screen_Max_Y
  4406.  
  4407.         pushfd
  4408.         cli
  4409.  
  4410.         mov     [_display.lfb_pitch], ecx
  4411.  
  4412.         mov     [screen_workarea.right], eax
  4413.         mov     [screen_workarea.bottom], edx
  4414.  
  4415.         push    ebx
  4416.         push    esi
  4417.         push    edi
  4418.  
  4419.         pushad
  4420.  
  4421.         cmp     [do_not_touch_winmap], 1
  4422.         je      @f
  4423.  
  4424.         stdcall kernel_free, [_display.win_map]
  4425.  
  4426.         mov     eax, [_display.width]
  4427.         mul     [_display.height]
  4428.         mov     [_display.win_map_size], eax
  4429.  
  4430.         stdcall kernel_alloc, eax
  4431.         mov     [_display.win_map], eax
  4432.         test    eax, eax
  4433.         jz      .epic_fail
  4434. ; store for f.18.24
  4435.         mov     eax, [_display.width]
  4436.         mov     [display_width_standard], eax
  4437.  
  4438.         mov     eax, [_display.height]
  4439.         mov     [display_height_standard], eax
  4440. @@:
  4441.         call    calculate_fast_getting_offset_for_WinMapAddress
  4442. ; for Qemu or non standart video cards
  4443. ; Unfortunately [BytesPerScanLine] does not always
  4444. ;                             equal to [_display.width] * [ScreenBPP] / 8
  4445.         call    calculate_fast_getting_offset_for_LFB
  4446.         popad
  4447.  
  4448.         call    repos_windows
  4449.         xor     eax, eax
  4450.         xor     ebx, ebx
  4451.         mov     ecx, [_display.width]
  4452.         mov     edx, [_display.height]
  4453.         dec     ecx
  4454.         dec     edx
  4455.         call    calculatescreen
  4456.         pop     edi
  4457.         pop     esi
  4458.         pop     ebx
  4459.  
  4460.         popfd
  4461.         ret
  4462.  
  4463. .epic_fail:
  4464.         hlt                     ; Houston, we've had a problem
  4465.  
  4466. ; --------------- APM ---------------------
  4467. uglobal
  4468. apm_entry       dp      0
  4469. apm_vf          dd      0
  4470. endg
  4471.  
  4472. align 4
  4473. sys_apm:
  4474.         xor     eax, eax
  4475.         cmp     word [apm_vf], ax       ; Check APM BIOS enable
  4476.         jne     @f
  4477.         inc     eax
  4478.         or      dword [esp + 44], eax   ; error
  4479.         add     eax, 7
  4480.         mov     dword [esp + SYSCALL_STACK.eax], eax   ; 32-bit protected-mode interface not supported
  4481.         ret
  4482.  
  4483. @@:
  4484. ;       xchg    eax, ecx
  4485. ;       xchg    ebx, ecx
  4486.  
  4487.         cmp     dx, 3
  4488.         ja      @f
  4489.         and     [esp + 44], byte 0xfe    ; emulate func 0..3 as func 0
  4490.         mov     eax, [apm_vf]
  4491.         mov     [esp + SYSCALL_STACK.eax], eax
  4492.         shr     eax, 16
  4493.         mov     [esp + SYSCALL_STACK.ecx], eax
  4494.         ret
  4495.  
  4496. @@:
  4497.  
  4498.         mov     esi, [master_tab+(OS_BASE shr 20)]
  4499.         xchg    [master_tab], esi
  4500.         push    esi
  4501.         mov     edi, cr3
  4502.         mov     cr3, edi                ;flush TLB
  4503.  
  4504.         call    pword [apm_entry]       ;call APM BIOS
  4505.  
  4506.         xchg    eax, [esp]
  4507.         mov     [master_tab], eax
  4508.         mov     eax, cr3
  4509.         mov     cr3, eax
  4510.         pop     eax
  4511.  
  4512.         mov     [esp + SYSCALL_STACK.edi], edi
  4513.         mov     [esp + SYSCALL_STACK.esi], esi
  4514.         mov     [esp + SYSCALL_STACK.ebx], ebx
  4515.         mov     [esp + SYSCALL_STACK.edx], edx
  4516.         mov     [esp + SYSCALL_STACK.ecx], ecx
  4517.         mov     [esp + SYSCALL_STACK.eax], eax
  4518.         setc    al
  4519.         and     [esp + 44], byte 0xfe
  4520.         or      [esp + 44], al
  4521.         ret
  4522. ; -----------------------------------------
  4523.  
  4524. align 4
  4525. undefined_syscall:                      ; Undefined system call
  4526.         mov     [esp + SYSCALL_STACK.eax], dword -1
  4527.         ret
  4528.  
  4529. align 4
  4530. ; @brief Check if given memory region lays in lower 2gb (userspace memory) or not
  4531. ; @param base Base address of region
  4532. ; @param len Lenght of region
  4533. ; @return ZF = 1 if region in userspace memory,
  4534. ;         ZF = 0 otherwise
  4535. proc is_region_userspace stdcall, base:dword, len:dword
  4536.         push    eax
  4537.         mov     eax, [base]
  4538.  
  4539.         cmp     eax, OS_BASE-1
  4540.         ja      @f              ; zf
  4541.  
  4542.         add     eax, [len]
  4543.         jc      @f              ; zf
  4544.         cmp     eax, OS_BASE
  4545.         ja      @f              ; zf
  4546.  
  4547.         cmp     eax, eax        ; ZF
  4548. @@:
  4549.         pop     eax
  4550.         ret
  4551. endp
  4552.  
  4553. align 4
  4554. ; @brief Check whether given string lays in userspace memory, i.e. below OS_BASE
  4555. ; @param base Base address of string
  4556. ; @return ZF = 1 if string in userspace memory,
  4557. ;         zf = 0 otherwise
  4558. proc is_string_userspace stdcall, base:dword
  4559.         push    eax ecx edi
  4560.         xor     eax, eax
  4561.         mov     edi, [base]
  4562.  
  4563.         mov     ecx, OS_BASE-1
  4564.         sub     ecx, edi
  4565.         jb      .done           ; zf
  4566.         inc     ecx
  4567.         cmp     ecx, 0x10000    ; don't allow strings larger than 64k?
  4568.         jbe     @f
  4569.         mov     ecx, 0x10000
  4570. @@:
  4571.         repnz scasb
  4572. .done:
  4573.         pop     edi ecx eax
  4574.         ret
  4575. endp
  4576.  
  4577. if ~ lang eq sp
  4578. diff16 "end of .text segment",0,$
  4579. end if
  4580.  
  4581. include "data32.inc"
  4582.  
  4583. __REV__ = __REV
  4584.  
  4585. if ~ lang eq sp
  4586. diff16 "end of kernel code",0,$
  4587. end if
  4588.