Subversion Repositories Kolibri OS

Rev

Rev 10002 | Details | Compare with Previous | Last modification | View Log | RSS feed

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