about summary refs log tree commit diff stats
path: root/dev/c/system.html
blob: c91dc34bb2eb8c136f2a2e0c63d566ec6e8f56ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<!DOCTYPE html>
<html dir="ltr" lang="en">
    <head>
        <meta charset='utf-8'>
        <title>System Development &amp; GDB</title>
    </head>
    <body>
        <a href="../index.html">Development Index</a>

        <h1>System Development</h1>

        <p>System development requires knowing how to debug
        kernel know how, in this example will be used Qemu
        and GDB. Qemu creates the virtual machine that kernel
        will run on and GDB will connect to it to help us
        understand how things tick.</p>

        <h2>Kernel Build</h2>

        <pre>
        $ tar xf linux-4.9.48.tar.xz
        $ cd linux-4.9.48
        </pre>

        <p>Default configuration disable some security
        configurations that allow us to debug (random memory
        layout).</p>

        <pre>
        $ make x86_64_defconfig
        </pre>

        <p>Enable CONFIG_DEBUG_INFO, CONFIG_DEBUG_INFO_DWARF4
        and CONFIG_GDB_SCRIPTS in the kernel;</p>

        <pre>
        make x86_64_defconfig
        cat &lt;&lt;EOF &gt;.config-fragment
        CONFIG_DEBUG_INFO=y
        CONFIG_DEBUG_KERNEL=y
        CONFIG_GDB_SCRIPTS=y
        EOF
        ./scripts/kconfig/merge_config.sh .config .config-fragment
        </pre>

        <p>Check or change to your needs the configuration;</p>
        <pre>
        $ make nconfig
        </pre>

        <p>Build Kernel and modules;</p>

        <pre>
        $ make -j $(nproc)
        </pre>

        <h2>Simple Init</h2>

        <p>Now that you have the kernel compiled you can
        create a simple init program, this program is called
        when kernel finish to load and setup its internals
        and is ready to launch first process.
        Init program should not exit or kernel will panic.
        Create init.S;</p>

        <pre>
        .global _start
        _start:
            mov $1, %rax
            mov $1, %rdi
            mov $message, %rsi
            mov $message_len, %rdx
            syscall
            jmp .
            message: .ascii "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n"
            .equ message_len, . - message
        </pre>

        <p>Assemble, link and create simple initial ram disk;</p>

        <pre>
        mkdir d
        as --64 -o init.o init.S # assemble
        ld -o d/init init.o      # link
        cd d
        find . | cpio -o -H newc | gzip > ../rootfs.cpio.gz
        </pre>

        <p>Clean temporary directory;</p>

        <pre>
        cd ..
        rm -r d/
        </pre>

        <p>Can be used C to create init program;</p>

        <pre>
        #include &lt;stdio.h&gt;
        #include &lt;unistd.h&gt;

        int main() {
            printf("FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR\n");
            sleep(0xFFFFFFFF);
            return 0;
        }
        </pre>

        <pre>
        $ gcc -static init.c -o init
        </pre>

        <h2>Start Debugging</h2>

        <p>Test qemu, kernel and simple init program, you
        should see
        "FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR FOOBAR";</p>

        <pre>
        $ qemu-system-x86_64 -enable-kvm --kernel arch/x86_64/boot/bzImage \
        --initrd rootfs.cpio.gz
        </pre>

        <p>If everything goes well you can start qemu without
        starting the CPU (-S) and with gdb server on TCP port 1234 (-s).</p>

        <pre>
        $ qemu-system-x86_64 -enable-kvm --kernel arch/x86/boot/bzImage \
                --initrd rootfs.cpio.gz \
                -S -s
        </pre>

        <p>On another terminal start gdb;</p>

        <pre>
        gdb \
            -ex "add-auto-load-safe-path $(pwd)" \
            -ex "file vmlinux" \
            -ex 'set arch i386:x86-64:intel' \
            -ex 'target remote localhost:1234' \
            -ex 'break start_kernel' \
            -ex 'continue' \
            -ex 'disconnect' \
            -ex 'set arch i386:x86-64' \
            -ex 'target remote localhost:1234'
        </pre>

        <pre>
	(gdb) info thread
	  Id   Target Id         Frame
	* 1    Thread 1 (CPU#0 [running]) start_kernel () at init/main.c:480
	</pre>

	<pre>
	(gdb) info frame
	Stack level 0, frame at 0xffffffff81e03f90:
	 rip = 0xffffffff81f4db2d in start_kernel (init/main.c:480); saved rip = 0xffffffff81f4d28e
	 called by frame at 0xffffffff81e03fa0
	 source language c.
	 Arglist at 0xffffffff81e03f80, args:
	 Locals at 0xffffffff81e03f80, Previous frame's sp is 0xffffffff81e03f90
	 Saved registers:
	  rip at 0xffffffff81e03f88
	</pre>

	<pre>
	(gdb) print $rip
	$2 = (void (*)()) 0xffffffff81f4db2d &lt;start_kernel&gt;
	(gdb)
	</pre>

        <a href="../index.html">Development Index</a>
        <p>
        This is part of the c9-doc Manual.
        Copyright (C) 2016
        c9 team.
        See the file <a href="../../fdl-1.3-standalone.html">Gnu Free Documentation License</a>
        for copying conditions.</p>

    </body>

</html>
fd6f'>b7f19942 ^
3360c8b7 ^

fc730139 ^


b7f19942 ^

2edbb7b0 ^
eb16da9f ^


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
              
             
 
                     

                         
                

    
                                      
 
                                         
                       
                               
 
                                                
                            
 

                                                     
 



















                                                                    
                                                                      









                            






                                                        

                                                 










                                                                                


                                      



                                        

                                      
                                   
                        



























                                                                                         
















                                                                                                       

                            
                    



































                                                                       
 


                                                                              
                                                                     

                                         
                                                                 

                     
                                                                  
                  




                                                      
                                               




                                                   
                                                        

                           

                                                 


                                       

                                                     
           


                                                 
import streams
import tables

import css/mediaquery
import css/cssparser
import css/selectorparser
import html/tags

type
  CSSRuleBase* = ref object of RootObj

  CSSRuleDef* = ref object of CSSRuleBase
    sels*: SelectorList
    decls*: seq[CSSDeclaration]

  CSSConditionalDef* = ref object of CSSRuleBase
    children*: CSSStylesheet

  CSSMediaQueryDef* = ref object of CSSConditionalDef
    query*: MediaQueryList

  CSSStylesheet* = ref object
    mq_list*: seq[CSSMediaQueryDef]
    tag_table*: array[TagType, seq[CSSRuleDef]]
    id_table*: TableRef[string, seq[CSSRuleDef]]
    class_table*: TableRef[string, seq[CSSRuleDef]]
    general_list*: seq[CSSRuleDef]
    len*: int

type SelectorHashes = object
  tag: TagType
  id: string
  class: string

func newStylesheet*(cap: int): CSSStylesheet =
  new(result)
  let bucketsize = cap div 2
  result.id_table = newTable[string, seq[CSSRuleDef]](bucketsize)
  result.class_table = newTable[string, seq[CSSRuleDef]](bucketsize)
  result.general_list = newSeqOfCap[CSSRuleDef](bucketsize)

proc getSelectorIds(hashes: var SelectorHashes, sel: Selector): bool =
  case sel.t
  of TYPE_SELECTOR:
    hashes.tag = sel.tag
    return true
  of CLASS_SELECTOR:
    hashes.class = sel.class
    return true
  of ID_SELECTOR:
    hashes.id = sel.id
    return true
  of ATTR_SELECTOR, PSELEM_SELECTOR, UNIVERSAL_SELECTOR:
    return false
  of COMBINATOR_SELECTOR:
    for sel in sel.csels[^1]:
      if hashes.getSelectorIds(sel):
        return true
    return false
  of PSEUDO_SELECTOR:
    if sel.pseudo.t in {PSEUDO_IS, PSEUDO_WHERE}:
      # Basically just hash whatever the selectors have in common:
      #1. get the hashable values of selector 1
      #2. for every other selector x:
      #3.   get hashable values of selector x
      #4.   store hashable values of selector x that aren't stored yet
      #5.   for every hashable value of selector 1 that doesn't match selector x
      #6.     cancel hashable value
      var cancel_tag = false
      var cancel_id = false
      var cancel_class = false
      var i = 0
      if i < sel.pseudo.fsels.len:
        let list = sel.pseudo.fsels[i]
        for sel in list:
          if hashes.getSelectorIds(sel):
            break
        inc i

      while i < sel.pseudo.fsels.len:
        let list = sel.pseudo.fsels[i]
        var nhashes: SelectorHashes
        for sel in list:
          if nhashes.getSelectorIds(sel):
            break
        if hashes.tag == TAG_UNKNOWN:
          hashes.tag = nhashes.tag
        elif not cancel_tag and nhashes.tag != TAG_UNKNOWN and nhashes.tag != hashes.tag:
          cancel_tag = true

        if hashes.id == "":
          hashes.id = nhashes.id
        elif not cancel_id and nhashes.id != "" and nhashes.id != hashes.id:
          cancel_id = true

        if hashes.class == "":
          hashes.class = nhashes.class
        elif not cancel_class and nhashes.class != "" and nhashes.class != hashes.class:
          cancel_class = true

        inc i

      if cancel_tag:
        hashes.tag = TAG_UNKNOWN
      if cancel_id:
        hashes.id = ""
      if cancel_class:
        hashes.class = ""

      if hashes.tag != TAG_UNKNOWN or hashes.id != "" or hashes.class != "":
        return true

iterator gen_rules*(sheet: CSSStylesheet, tag: TagType, id: string, classes: seq[string]): CSSRuleDef =
  for rule in sheet.tag_table[tag]:
    yield rule
  if id != "":
    if sheet.id_table.hasKey(id):
      for rule in sheet.id_table[id]:
        yield rule
  if classes.len > 0:
    for class in classes:
      if sheet.class_table.hasKey(class):
        for rule in sheet.class_table[class]:
          yield rule
  for rule in sheet.general_list:
    yield rule

proc add(sheet: var CSSStylesheet, rule: CSSRuleDef) =
  var hashes: SelectorHashes
  for list in rule.sels:
    for sel in list:
      if hashes.getSelectorIds(sel):
        break

    if hashes.tag != TAG_UNKNOWN:
      sheet.tag_table[hashes.tag].add(rule)
    elif hashes.id != "":
      if hashes.id notin sheet.id_table:
        sheet.id_table[hashes.id] = newSeq[CSSRuleDef]()
      sheet.id_table[hashes.id].add(rule)
    elif hashes.class != "":
      if hashes.class notin sheet.class_table:
        sheet.class_table[hashes.class] = newSeq[CSSRuleDef]()
      sheet.class_table[hashes.class].add(rule)
    else:
      sheet.general_list.add(rule)

proc add*(sheet: var CSSStylesheet, rule: CSSRuleBase) {.inline.} =
  if rule of CSSRuleDef:
    sheet.add(CSSRuleDef(rule))
  else:
    sheet.mq_list.add(CSSMediaQueryDef(rule))
  inc sheet.len

proc add*(sheet: var CSSStylesheet, sheet2: CSSStylesheet) {.inline.} =
  sheet.general_list.add(sheet2.general_list)
  for tag in TagType:
    sheet.tag_table[tag].add(sheet2.tag_table[tag])
  for key, value in sheet2.id_table.pairs:
    if key notin sheet.id_table:
      sheet.id_table[key] = newSeq[CSSRuleDef]()
    sheet.id_table[key].add(value)
  for key, value in sheet2.class_table.pairs:
    if key notin sheet.class_table:
      sheet.class_table[key] = newSeq[CSSRuleDef]()
    sheet.class_table[key].add(value)
  sheet.len += sheet2.len

proc getDeclarations(rule: CSSQualifiedRule): seq[CSSDeclaration] {.inline.} =
  rule.oblock.value.parseListOfDeclarations2()

proc addRule(stylesheet: var CSSStylesheet, rule: CSSQualifiedRule) =
  let sels = parseSelectors(rule.prelude)
  if sels.len > 1 or sels[^1].len > 0:
    let r = CSSRuleDef(sels: sels, decls: rule.getDeclarations())
    stylesheet.add(r)

proc addAtRule(stylesheet: var CSSStylesheet, atrule: CSSAtRule) =
  case atrule.name
  of "media":
    let query = parseMediaQueryList(atrule.prelude)
    let rules = atrule.oblock.value.parseListOfRules()
    if rules.len > 0:
      var media = CSSMediaQueryDef()
      media.children = newStylesheet(rules.len)
      media.query = query
      for rule in rules:
        if rule of CSSAtRule:
          media.children.addAtRule(CSSAtRule(rule))
        else:
          media.children.addRule(CSSQualifiedRule(rule))
      stylesheet.add(media)
  else: discard #TODO

proc parseStylesheet*(s: Stream): CSSStylesheet =
  let css = parseCSS(s)
  result = newStylesheet(css.value.len)
  for v in css.value:
    if v of CSSAtRule: result.addAtRule(CSSAtRule(v))
    else: result.addRule(CSSQualifiedRule(v))
  s.close()

proc parseStylesheet*(s: string): CSSStylesheet =
  return newStringStream(s).parseStylesheet()