Subversion Repositories Kolibri OS

Rev

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