Subversion Repositories Kolibri OS

Rev

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