24.01.2014 Views

Codice

Codice

Codice

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

258 volume VI os16<br />

Script e sorgenti del kernel 259<br />

«<br />

«<br />

«<br />

«<br />

«<br />

1460010 | {<br />

1460011 | *timer = _clock_seconds;<br />

1460012 | }<br />

1460013 | return (_clock_seconds);<br />

1460014 |}<br />

104.6.12 kernel/k_libc/k_vprintf.c<br />

Si veda la sezione 103.5.<br />

1470001 |#include <br />

1470002 |#include <br />

1470003 |#include <br />

1470004 |#include <br />

1470005 |#include <br />

1470006 |//----------------------------------------------------------------------<br />

1470007 |int<br />

1470008 |k_vprintf (const char *restrict format, va_list arg)<br />

1470009 |{<br />

1470010 | char string[BUFSIZ];<br />

1470011 | int ret;<br />

1470012 | string[0] = 0;<br />

1470013 | ret = k_vsprintf (string, format, arg);<br />

1470014 | dev_io ((pid_t) 0, DEV_CONSOLE, DEV_WRITE, (off_t) 0, string,<br />

1470015 | strlen (string), NULL);<br />

1470016 | return ret;<br />

1470017 |}<br />

104.6.13 kernel/k_libc/k_vsprintf.c<br />

Si veda la sezione 103.5.<br />

1480001 |#include <br />

1480002 |#include <br />

1480003 |#include <br />

1480004 |//----------------------------------------------------------------------<br />

1480005 |int<br />

1480006 |k_vsprintf (char *restrict string, const char *restrict format,<br />

1480007 | va_list arg)<br />

1480008 |{<br />

1480009 | int ret;<br />

1480010 | ret = vsnprintf (string, BUFSIZ, format, arg);<br />

1480011 | return ret;<br />

1480012 |}<br />

104.7 os16: «kernel/main.h»<br />

Si veda la sezione 103.6.<br />

1490001 |#ifndef _KERNEL_MAIN_H<br />

1490002 |#define _KERNEL_MAIN_H 1<br />

1490003 |<br />

1490004 |#include <br />

1490005 |<br />

1490006 |void menu (void);<br />

1490007 |pid_t run (char *path, char *argv[], char *envp[]);<br />

1490008 |int main (int argc, char *argv[], char *envp[]);<br />

1490009 |<br />

1490010 |#endif<br />

104.7.1 kernel/main/build.h<br />

Si veda la sezione 103.6.<br />

1500001 |#define BUILD_DATE "2010.07.26 16:33:58"<br />

104.7.2 kernel/main/crt0.s<br />

Si veda la sezione 94.2.2.<br />

1510001 |.extern _main<br />

1510002 |.global ___mkargv<br />

1510003 |;-----------------------------------------------------------------------<br />

1510004 |; Please note that, at the beginning, all the segment registers are<br />

1510005 |; the same: CS==DS==ES==SS. But the data segments (DS, ES, SS) are meant<br />

1510006 |; to be separated from the code, and they starts at: CS + __segoff.<br />

1510007 |; The label "__segoff" is replaced by the linker with a constant value<br />

1510008 |; (inside the code) with the segment offset to add to CS; this way<br />

1510009 |; it is possibile to find where DS and ES should start.<br />

1510010 |;-----------------------------------------------------------------------<br />

1510011 |; The following statement says that the code will start at "startup"<br />

1510012 |; label.<br />

1510013 |;-----------------------------------------------------------------------<br />

1510014 |entry startup<br />

1510015 |;-----------------------------------------------------------------------<br />

1510016 |.text<br />

1510017 |;-----------------------------------------------------------------------<br />

1510018 |startup:<br />

1510019 | ;<br />

1510020 | ; Jump after initial data.<br />

1510021 | ;<br />

1510022 | jmp startup_code<br />

1510023 | ;<br />

1510024 |filler:<br />

1510025 | ;<br />

1510026 | ; After four bytes, from the start, there is the<br />

1510027 | ; magic number and other data.<br />

1510028 | ;<br />

1510029 | .space (0x0004 - (filler - startup))<br />

1510030 |magic:<br />

1510031 | ;<br />

1510032 | ; Add to "/etc/magic" the following line:<br />

1510033 | ;<br />

1510034 | ; 4 quad 0x6B65726E6F733136 os16 kernel<br />

1510035 | ;<br />

1510036 | .data4 0x6F733136 ; os16<br />

1510037 | .data4 0x6B65726E ; kern<br />

1510038 | ;<br />

1510039 |segoff: ;<br />

1510040 | .data2 __segoff ; These values, for a kernel image,<br />

1510041 |etext: ; are not used.<br />

1510042 | .data2 __etext ;<br />

1510043 |edata: ;<br />

1510044 | .data2 __edata ;<br />

1510045 |ebss: ;<br />

1510046 | .data2 __end ;<br />

1510047 |stack_size: ;<br />

1510048 | .data2 0x0000 ;<br />

1510049 | ;<br />

1510050 | ; At the next label, the work begins.<br />

1510051 | ;<br />

1510052 |.align 2<br />

1510053 |startup_code:<br />

1510054 | ;<br />

1510055 | ; Check where we are. If we are at segment 0x1000,<br />

1510056 | ; then move to 0x3000.<br />

1510057 | ;<br />

1510058 | mov cx, cs<br />

1510059 | xor cx, #0x1000<br />

1510060 | jcxz move_code_from_0x1000_to_0x3000<br />

1510061 | ;<br />

1510062 | ; Check where we are. If we are at segment 0x3000,<br />

1510063 | ; then move to 0x0050, preserving the IVT and the BDA.<br />

1510064 | ;<br />

1510065 | mov cx, cs<br />

1510066 | xor cx, #0x3000<br />

1510067 | jcxz move_code_from_0x3000_to_0x0050<br />

1510068 | ;<br />

1510069 | ; Check where we are. If we are at segment 0x1050,<br />

1510070 | ; then jump to the main code.<br />

1510071 | ;<br />

1510072 | mov cx, cs<br />

1510073 | xor cx, #0x1050<br />

1510074 | jcxz main_code<br />

1510075 | ;<br />

1510076 | ; Otherwise, just halt.<br />

1510077 | ;<br />

1510078 | hlt<br />

1510079 | jmp startup_code<br />

1510080 | ;<br />

1510081 |move_code_from_0x1000_to_0x3000:<br />

1510082 | ;<br />

1510083 | cld ; Clear direction flag.<br />

1510084 | mov ax, #0x3000 ; Set ES as the destination segment.<br />

1510085 | mov es, ax ;<br />

1510086 | mov ax, #0x1000 ; Set DS as the source segment.<br />

1510087 | mov ds, ax ;<br />

1510088 | ;<br />

1510089 | mov cx, #0x8000 ; Move 32768 words = 65536 byte (64 Kibyte).<br />

1510090 | mov si, #0x0000 ; DS:SI == Source pointer<br />

1510091 | mov di, #0x0000 ; ES:DI == Destination pointer<br />

1510092 | rep<br />

1510093 | movsw ; Copy the array of words<br />

1510094 | ;<br />

1510095 | mov ax, #0x4000 ; Set ES as the destination segment.<br />

1510096 | mov es, ax ;<br />

1510097 | mov ax, #0x2000 ; Set DS as the source segment.<br />

1510098 | mov ds, ax ;<br />

1510099 | ;<br />

1510100 | mov cx, #0x8000 ; Move 32768 words = 65536 byte (64 Kibyte).<br />

1510101 | mov si, #0x0000 ; DS:SI == Source pointer<br />

1510102 | mov di, #0x0000 ; ES:DI == Destination pointer<br />

1510103 | rep<br />

1510104 | movsw ; Copy the array of words<br />

1510105 | ;<br />

1510106 | jmp far #0x3000:#0x0000 ; Go to the new kernel copy.<br />

1510107 | ;<br />

1510108 |move_code_from_0x3000_to_0x0050:<br />

1510109 | cld ; Clear direction flag.<br />

1510110 | ;<br />

1510111 | ; Text (instructions) is moved at segment 0x1050 (address 0x10500).<br />

1510112 | ;<br />

1510113 | mov ax, #0x1050 ; Set ES as the destination segment.<br />

1510114 | mov es, ax ;<br />

1510115 | mov ax, #0x3000 ; Set DS as the source segment.<br />

1510116 | mov ds, ax ;<br />

1510117 | ;<br />

1510118 | mov cx, #0x8000 ; Move 32768 words = 65536 byte (64 Kibyte).<br />

1510119 | mov si, #0x0000 ; DS:SI == Source pointer<br />

1510120 | mov di, #0x0000 ; ES:DI == Destination pointer<br />

1510121 | rep<br />

1510122 | movsw ; Copy the array of words<br />

1510123 | ;<br />

1510124 | ; Data is moved at segment 0x0050 (address 0x00500), before the<br />

1510125 | ; text segment.<br />

1510126 | ;<br />

1510127 | mov ax, #0x0050 ; Set ES as the destination segment.<br />

1510128 | mov es, ax ;<br />

1510129 | mov ax, #0x3000 ; Calculate where is the data segment:<br />

1510130 | add ax, #__segoff ; it is at 0x3000 + __segoff.<br />

1510131 | mov ds, ax ; Set DS as the source segment.<br />

1510132 | ;<br />

1510133 | mov cx, #0x8000 ; Move 32768 words = 65536 byte (64 Kibyte).<br />

1510134 | mov si, #0x0000 ; DS:SI == Source pointer<br />

1510135 | mov di, #0x0000 ; ES:DI == Destination pointer<br />

1510136 | rep<br />

1510137 | movsw ; Copy the array of words<br />

1510138 | ;<br />

1510139 | jmp far #0x1050:#0x0000 ; Go to the new kernel copy.<br />

1510140 | ;<br />

1510141 |;-----------------------------------------------------------------------<br />

1510142 |main_code:<br />

1510143 | ;<br />

1510144 | ; Fix data segments!<br />

1510145 | ;<br />

1510146 | mov ax, #0x0050<br />

1510147 | mov ds, ax<br />

1510148 | mov ss, ax<br />

1510149 | mov es, ax<br />

1510150 | ;<br />

1510151 | ; Fix SP at the kernel stack bottom: the effective stack pointer<br />

1510152 | ; value should be 0x10000, but only 0x0000 can be written. At the<br />

1510153 | ; first push SP reaches 0xFFFE.<br />

1510154 | ;<br />

1510155 | mov sp, #0x0000<br />

1510156 | ;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!