source: rtems-libbsd/libbsd.txt @ 0f5dd1c

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 0f5dd1c was 0f5dd1c, checked in by Sebastian Huber <sebastian.huber@…>, on 05/06/16 at 05:27:25

Add script to create the kernel namespace header

  • Property mode set to 100644
File size: 40.5 KB
Line 
1RTEMS BSD Library Guide
2=======================
3:toc:
4:icons:
5:numbered:
6:website: http://www.rtems.org/
7
8RTEMS uses FreeBSD 9.2 as the source of its TCP/IP and USB stacks.
9This is a guide which captures information on the
10process of merging code from FreeBSD, building this library,
11RTEMS specific support files, and general guidelines on what
12modifications to the FreeBSD source are permitted.
13
14Goals of this effort are
15
16* update TCP/IP and provide USB in RTEMS,
17* ease updating to future FreeBSD versions,
18* ease tracking changes in FreeBSD code,
19* minimize manual changes in FreeBSD code, and
20* define stable kernel/device driver API which is implemented
21by both RTEMS and FreeBSD. This is the foundation of the port.
22
23We will work to push our changes upstream to the FreeBSD Project
24and minimize changes required at each update point.
25
26*******************************************************************************
27This is a work in progress and is very likely to be incomplete.
28Please help by adding to it.
29*******************************************************************************
30
31== Getting Started
32
33=== Tool Chain ===
34
35You need a tool chain for RTEMS based on at least RSB 4.12 April 2016 or later.
36=== Installation Overview ===
37
38. You must configure your BSP with the +--disable-networking+ option to disable
39the old network stack.  Make sure no header files of the old network stack are
40installed.
41
42. Clone the Git repository +git clone git://git.rtems.org/rtems-libbsd.git+.
43. Change into the RTEMS BSD library root directory.
44. Edit the `config.inc` configuration file and adjust it to your environment.
45. Run +waf configure ...+.
46. Run +waf+.
47. Run +waf install+.
48
49Refer to the README.waf for Waf building instructions.
50
51Make sure the submodules have been initialised and are updated. If a 'git
52status' says `rtems_waf` need updating run the submodule update command:
53
54 $ git submodule rtems_waf update
55
56=== Board Support Package Requirements ===
57
58The RTEMS version must be at least 4.11.  The Board Support Package (BSP)
59should support the
60http://www.rtems.org/onlinedocs/doxygen/cpukit/html/group\__rtems\__interrupt__extension.html[Interrupt Manager Extension]
61// The first underscores have to be masked to stop asciidoc interpreting them
62to make use of generic FreeBSD based drivers.
63
64The linker command file of the BSP must contain the following section
65definitions:
66
67-------------------------------------------------------------------------------
68.rtemsroset : {
69        KEEP (*(SORT(.rtemsroset.*)))
70}
71
72.rtemsrwset : {
73        KEEP (*(SORT(.rtemsrwset.*)))
74}
75-------------------------------------------------------------------------------
76
77The first output section can be placed in read-only memory.  The second output
78section must be placed in read-write memory.  The output section name is not
79relevant.  The output sections may also contain other input sections.
80
81=== Board Support Package Configuration and Build ===
82
83You need to configure RTEMS for the desired BSP and install it.  The BSP should
84be configured with a disabled network stack.  The BSD library containing the
85new network stack is a separate package.  Using a BSP installation containing
86the old network stack may lead to confusion and unpredictable results.
87
88The following script is used to build the `arm/realview_pbx_a9_qemu` BSP for
89our internal testing purposes:
90
91-------------------------------------------------------------------------------
92#!/bin/sh
93
94cd ${HOME}/sandbox
95rm -rf b-realview_pbx_a9_qemu
96mkdir b-realview_pbx_a9_qemu
97cd b-realview_pbx_a9_qemu
98${HOME}/git-rtems/configure \
99        --prefix=${HOME}/sandbox/install \
100        --target=arm-rtems4.11 \
101        --enable-rtemsbsp=realview_pbx_a9_qemu \
102        --disable-networking && \
103        make && \
104        make install
105-------------------------------------------------------------------------------
106
107The `arm/realview_pbx_a9_qemu` BSP running on the Qemu simulator has some
108benefits for development and test of the BSD library
109
110* it offers a NULL pointer read and write protection,
111* Qemu is a fast simulator,
112* Qemu provides support for GDB watchpoints,
113* Qemu provides support for virtual Ethernet networks, e.g. TUN and bridge
114devices (you can run multiple test instances on one virtual network).
115
116=== BSD Library Configuration and Build ===
117
118The build system based on the Waf build system. To build with Waf please refer
119to the README.waf file.
120
121===== Example Configuration =====
122
123In the BSD library source directory edit the file `config.inc`.  Continuing on
124the above, the `config.inc` used to match the above is:
125
126-------------------------------------------------------------------------------
127# Mandatory: Select your BSP and installation prefix
128TARGET = arm-rtems4.11
129BSP = realview_pbx_a9_qemu
130PREFIX = $(HOME)/sandbox/install
131
132# Optional: Separate installation base directory
133INSTALL_BASE = $(PREFIX)/$(TARGET)/$(BSP)
134
135# Optional: Network test configuration
136TEST_RUNNER = $(BSP)
137NET_CFG_SELF_IP = 10.0.0.2
138NET_CFG_NETMASK = 255.255.0.0
139NET_CFG_PEER_IP = 10.0.0.1
140NET_CFG_GATEWAY_IP = 10.0.0.1
141NET_TAP_INTERFACE = tap0
142-------------------------------------------------------------------------------
143
144=== BSD Library Initialization ===
145
146Use the following code to initialize the BSD library:
147
148-------------------------------------------------------------------------------
149#include <assert.h>
150#include <sysexits.h>
151
152#include <machine/rtems-bsd-commands.h>
153#include <rtems/bsd/bsd.h>
154
155static void
156network_ifconfig_lo0(void)
157{
158        int exit_code;
159        char *lo0[] = {
160                "ifconfig",
161                "lo0",
162                "inet",
163                "127.0.0.1",
164                "netmask",
165                "255.255.255.0",
166                NULL
167        };
168        char *lo0_inet6[] = {
169                "ifconfig",
170                "lo0",
171                "inet6",
172                "::1",
173                "prefixlen",
174                "128",
175                NULL
176        };
177
178        exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(lo0), lo0);
179        assert(exit_code == EX_OK);
180
181        exit_code = rtems_bsd_command_ifconfig(RTEMS_BSD_ARGC(lo0_inet6), lo0_inet6);
182        assert(exit_code == EX_OK);
183}
184
185void
186network_init(void)
187{
188        rtems_status_code sc;
189
190        sc = rtems_bsd_initialize();
191        assert(sc == RTEMS_SUCCESSFUL);
192
193        network_ifconfig_lo0();
194}
195-------------------------------------------------------------------------------
196
197This performs the basic network stack initialization with a loopback interface.
198Further initialization must be done using the standard BSD network
199configuration commands
200http://www.freebsd.org/cgi/man.cgi?query=ifconfig&apropos=0&sektion=8&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[IFCONFIG(8)]
201using `rtems_bsd_command_ifconfig()` and
202http://www.freebsd.org/cgi/man.cgi?query=route&apropos=0&sektion=8&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[ROUTE(8)]
203using `rtems_bsd_command_route()`.  For an example please have a look at
204`testsuite/include/rtems/bsd/test/default-network-init.h`.
205
206=== Task Priorities and Stack Size ===
207
208The default task priority is 96 for the interrupt server task (name "IRQS"), 98
209for the timer server task (name "TIME") and 100 for all other tasks.  The
210application may provide their own implementation of the
211`rtems_bsd_get_task_priority()` function (for example in the module which calls
212`rtems_bsd_initialize()`) if different values are desired.
213
214The task stack size is determined by the `rtems_bsd_get_task_stack_size()`
215function which may be provided by the application in case the default is not
216appropriate.
217
218=== Size for Allocator Domains ===
219
220The size for an allocator domain can be specified via the
221`rtems_bsd_get_allocator_domain_size()` function.  The application may provide
222their own implementation of the `rtems_bsd_get_allocator_domain_size()`
223function (for example in the module which calls `rtems_bsd_initialize()`) if
224different values are desired.  The default size is 8MiB for all domains.
225
226== Network Stack Features
227
228http://roy.marples.name/projects/dhcpcd/index[DHCPCD(8)]:: DHCP client
229
230https://developer.apple.com/library/mac/documentation/Networking/Reference/DNSServiceDiscovery_CRef/Reference/reference.html[dns_sd.h]:: DNS Service Discovery
231
232http://www.opensource.apple.com/source/mDNSResponder/mDNSResponder-320.10/mDNSCore/mDNSEmbeddedAPI.h[mDNS]:: Multi-Cast DNS
233
234http://www.freebsd.org/cgi/man.cgi?query=unix&sektion=4&apropos=0&manpath=FreeBSD+9.2-RELEASE[UNIX(4)]:: UNIX-domain protocol family
235
236http://www.freebsd.org/cgi/man.cgi?query=inet&sektion=4&apropos=0&manpath=FreeBSD+9.2-RELEASE[INET(4)]:: Internet protocol family
237
238http://www.freebsd.org/cgi/man.cgi?query=inet6&apropos=0&sektion=4&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[INET6(4)]:: Internet protocol version 6 family
239
240http://www.freebsd.org/cgi/man.cgi?query=tcp&apropos=0&sektion=4&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[TCP(4)]:: Internet Transmission Control Protocol
241
242http://www.freebsd.org/cgi/man.cgi?query=udp&apropos=0&sektion=4&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[UDP(4)]:: Internet User Datagram Protocol
243
244http://www.freebsd.org/cgi/man.cgi?query=route&sektion=4&apropos=0&manpath=FreeBSD+9.2-RELEASE[ROUTE(4)]:: Kernel packet forwarding database
245
246http://www.freebsd.org/cgi/man.cgi?query=bpf&apropos=0&sektion=4&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[BPF(4)]:: Berkeley Packet Filter
247
248http://www.freebsd.org/cgi/man.cgi?query=socket&apropos=0&sektion=2&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[SOCKET(2)]:: Create an endpoint for communication
249
250http://www.freebsd.org/cgi/man.cgi?query=kqueue&apropos=0&sektion=2&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[KQUEUE(2)]:: Kernel event notification mechanism
251
252http://www.freebsd.org/cgi/man.cgi?query=select&apropos=0&sektion=2&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[SELECT(2)]:: Synchronous I/O multiplexing
253
254http://www.freebsd.org/cgi/man.cgi?query=poll&apropos=0&sektion=2&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[POLL(2)]:: Synchronous I/O multiplexing
255
256http://www.freebsd.org/cgi/man.cgi?query=route&apropos=0&sektion=8&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[ROUTE(8)]:: Manually manipulate the routing tables
257
258http://www.freebsd.org/cgi/man.cgi?query=ifconfig&apropos=0&sektion=8&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[IFCONFIG(8)]:: Configure network interface parameters
259
260http://www.freebsd.org/cgi/man.cgi?query=netstat&apropos=0&sektion=1&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[NETSTAT(1)]:: Show network status
261
262http://www.freebsd.org/cgi/man.cgi?query=ping&apropos=0&sektion=8&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[PING(8)]:: Send ICMP ECHO_REQUEST packets to network hosts
263
264http://www.freebsd.org/cgi/man.cgi?query=ping6&apropos=0&sektion=8&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[PING6(8)]:: Send ICMPv6 ECHO_REQUEST packets to network hosts
265
266http://www.freebsd.org/cgi/man.cgi?query=sysctl&sektion=3&apropos=0&manpath=FreeBSD+9.2-RELEASE[SYSCTL(3)]:: Get or set system information
267
268http://www.freebsd.org/cgi/man.cgi?query=resolver&sektion=3&apropos=0&manpath=FreeBSD+9.2-RELEASE[RESOLVER(3)]:: Resolver routines
269
270http://www.freebsd.org/cgi/man.cgi?query=gethostbyname&sektion=3&apropos=0&manpath=FreeBSD+9.2-RELEASE[GETHOSTBYNAME(3)]:: Get network host entry
271
272== Network Interface Drivers
273
274=== Link Up/Down Events
275
276You can notifiy the application space of link up/down events in your network
277interface driver via the if_link_state_change(LINK_STATE_UP/LINK_STATE_DOWN)
278function.  The DHCPCD(8) client is a consumer of these events for example.
279Make sure that the interface flag IFF_UP and the interface driver flag
280IFF_DRV_RUNNING is set in case the link is up, otherwise ether_output() will
281return the error status ENETDOWN.
282
283== Shell Commands
284
285=== HOSTNAME(1)
286
287In addition to the standard options the RTEMS version of the HOSTNAME(1)
288command supports the -m flag to set/get the multicast hostname of the
289mDNS resolver instance.  See also rtems_mdns_sethostname() and
290rtems_mdns_gethostname().
291
292== Qemu
293
294Use the following script to set up a virtual network with three tap devices
295connected via one bridge device.
296
297-------------------------------------------------------------------------------
298#!/bin/sh -x
299
300user=`whoami`
301interfaces=(1 2 3)
302
303tap=qtap
304bri=qbri
305
306case $1 in
307        up)
308                sudo -i brctl addbr $bri
309                for i in ${interfaces[@]} ; do
310                        sudo -i tunctl -t $tap$i -u $user ;
311                        sudo -i ifconfig $tap$i up ;
312                        sudo -i brctl addif $bri $tap$i ;
313                done
314                sudo -i ifconfig $bri up
315                ;;
316        down)
317                for i in ${interfaces[@]} ; do
318                        sudo -i ifconfig $tap$i down ;
319                        sudo -i tunctl -d $tap$i ;
320                done
321                sudo -i ifconfig $bri down
322                sudo -i brctl delbr $bri
323                ;;
324esac
325-------------------------------------------------------------------------------
326
327Connect your Qemu instance to one of the tap devices, e.g.
328
329-------------------------------------------------------------------------------
330qemu-system-i386 -m 512 -boot a -cpu pentium3 \
331        -drive file=$HOME/qemu/pc386_fda,index=0,if=floppy,format=raw \
332        -drive file=fat:$HOME/qemu/hd,format=raw \
333        -net nic,model=e1000,macaddr=0e:b0:ba:5e:ba:11 \
334        -net tap,ifname=qtap1,script=no,downscript=no \
335        -nodefaults -nographic -serial stdio
336-------------------------------------------------------------------------------
337
338Make sure that each Qemu instance uses its own MAC address to avoid an address
339conflict (or otherwise use it as a test).
340
341To connect the Qemu instances with your local network use the following
342(replace 'eth0' with the network interface of your host).
343
344-------------------------------------------------------------------------------
345ifconfig eth0 0.0.0.0
346brctl addif qbri eth0
347dhclient qbri
348-------------------------------------------------------------------------------
349
350== Issues and TODO
351
352* PCI support on x86 uses a quick and dirty hack, see pci_reserve_map().
353
354* Priority queues are broken with clustered scheduling.
355
356* Per-CPU data should be enabled once the new stack is ready for SMP.
357
358* Per-CPU NETISR(9) should be enabled onece the new stack is ready for SMP.
359
360* Multiple routing tables are not supported.  Every FIB value is set to zero
361  (= BSD_DEFAULT_FIB).
362
363* Process identifiers are not supported.  Every PID value is set to zero
364  (= BSD_DEFAULT_PID).
365
366* User credentials are not supported.  The following functions allow the
367  operation for everyone
368  - prison_equal_ip4(),
369  - chgsbsize(),
370  - cr_cansee(),
371  - cr_canseesocket() and
372  - cr_canseeinpcb().
373
374* A basic USB functionality test that is known to work on Qemu is desirable.
375
376* Adapt generic IRQ PIC interface code to Simple Vectored Interrupt Model
377  so that those architectures can use new TCP/IP and USB code.
378
379* freebsd-userspace/rtems/include/sys/syslog.h is a copy from the old
380  RTEMS TCP/IP stack. For some reason, the __printflike markers do not
381  compile in this environment. We may want to use the FreeBSD syslog.h
382  and get this addressed.
383
384* in_cksum implementations for architectures not supported by FreeBSD.
385  This will require figuring out where to put implementations that do
386  not originate from FreeBSD and are populated via the script.
387
388* MAC support functions are not thread-safe ("freebsd/lib/libc/posix1e/mac.c").
389
390* IFCONFIG(8): IEEE80211 support is disabled.  This module depends on a XML
391  parser and mmap().
392
393* get_cyclecount(): The implementation is a security problem.
394
395* What to do with the priority parameter present in the FreeBSD synchronization
396  primitives and the thread creation functions?
397
398* TASKQUEUE(9): Support spin mutexes.
399
400* ZONE(9): Review allocator lock usage in rtems-bsd-chunk.c.
401
402* KQUEUE(2): Choose proper lock for global kqueue list.
403
404* TIMEOUT(9): Maybe use special task instead of timer server to call
405  callout_tick().
406
407* sysctl_handle_opaque(): Implement reliable snapshots.
408
409* PING6(8): What to do with SIGALARM?
410
411* <sys/param.h>: Update Newlib to use a MSIZE of 256.
412
413* BPF(4): Add support for zero-copy buffers.
414
415* UNIX(4): Fix race conditions in the area of socket object and file node
416  destruction.  Add support for file descriptor transmission via control
417  messages.
418
419* PRINTF(9): Add support for log(), the %D format specifier is missing in the
420  normal printf() family.
421
422* Why is the interrupt server used?  The BSD interrupt handlers can block on
423synchronization primitives like mutexes.  This is in contrast to RTEMS
424interrupt service routines.  The BSPs using the generic interrupt support must
425implement the `bsp_interrupt_vector_enable()` and
426`bsp_interrupt_vector_disable()` routines.  They normally enable/disable a
427particular interrupt source at the interrupt controller.  This can be used to
428implement the interrupt server.  The interrupt server is a task that wakes-up
429in case an associated interrupt happens.  The interrupt source is disabled in
430a generic interrupt handler that wakes-up the interrupt server task.   Once the
431postponed interrupt processing is performed in the interrupt server the
432interrupt source is enabled again.
433
434* Convert all BSP linkcmds to use a linkcmds.base so the sections are
435easier to insert.
436
437* NIC Device Drivers
438- Only common PCI NIC drivers have been included in the initial set. These
439do not include any system on chip or ISA drivers.
440- PCI configuration probe does not appear to happen to determine if a
441NIC is in I/O or memory space. We have worked around this by using a
442static hint to tell the fxp driver the correct mode. But this needs to
443be addressed.
444- The ISA drivers require more BSD infrastructure to be addressed. This was
445outside the scope of the initial porting effort.
446
447== FreeBSD Source
448
449You should be able to rely on FreebSD manual pages and documentation
450for details on the code itself.
451
452=== Automatically Generated FreeBSD Files
453
454Some source and header files are automatically generated during the FreeBSD
455build process.  The `Makefile.todo` file performs this manually.  The should be
456included in `freebsd-to-rtems.py` script some time in the future.  For details,
457see also
458http://www.freebsd.org/cgi/man.cgi?query=kobj&sektion=9&apropos=0&manpath=FreeBSD+9.2-RELEASE[KOBJ(9)].
459
460=== Rules for Modifying FreeBSD Source
461
462Only add lines.  If your patch contains lines starting with a '-', then this is
463wrong.  Subtract code by added `#ifndef __rtems__`.  This makes merging easier
464in the future.  For example:
465
466-------------------------------------------------------------------------------
467/* Global variables for the kernel. */
468
469#ifndef __rtems__
470/* 1.1 */
471extern char kernelname[MAXPATHLEN];
472#endif /* __rtems__ */
473
474extern int tick;                        /* usec per tick (1000000 / hz) */
475-------------------------------------------------------------------------------
476
477-------------------------------------------------------------------------------
478#if defined(_KERNEL) || defined(_WANT_FILE)
479#ifdef __rtems__
480#include <rtems/libio_.h>
481#include <sys/fcntl.h>
482#endif /* __rtems__ */
483/*
484 * Kernel descriptor table.
485 * One entry for each open kernel vnode and socket.
486 *
487 * Below is the list of locks that protects members in struct file.
488 *
489 * (f) protected with mtx_lock(mtx_pool_find(fp))
490 * (d) cdevpriv_mtx
491 * none not locked
492 */
493-------------------------------------------------------------------------------
494
495-------------------------------------------------------------------------------
496extern int profprocs;                   /* number of process's profiling */
497#ifndef __rtems__
498extern volatile int ticks;
499#else /* __rtems__ */
500#include <rtems/score/watchdogimpl.h>
501#define ticks _Watchdog_Ticks_since_boot
502#endif /* __rtems__ */
503
504#endif /* _KERNEL */
505-------------------------------------------------------------------------------
506
507Add nothing (even blank lines) before or after the `__rtems__` guards.  Always
508include a `__rtems__` in the guards to make searches easy, so use
509
510* `#ifndef __rtems__`,
511* `#ifdef __rtems__`,
512* `#else /* __rtems__ */`, and
513* `#endif /* __rtems__ */`.
514
515For new code use
516http://www.freebsd.org/cgi/man.cgi?query=style&apropos=0&sektion=9&manpath=FreeBSD+9.2-RELEASE&arch=default&format=html[STYLE(9)].
517Do not format original FreeBSD code.
518
519== BSD Library Source
520
521=== What is in the Git Repository
522
523There is a self-contained kit with FreeBSD and RTEMS components pre-merged. The
524Waf wscript in this kit is automatically generated.
525
526Any changes to source in the `freebsd` directories will need to be merged
527upstream into our master FreeBSD checkout, the `freebsd-org` submodule.
528
529The repository contains two FreeBSD source trees.  In the `freebsd` directory
530are the so called 'managed' FreeBSD sources used to build the BSD library.  The
531FreeBSD source in `freebsd-org` is the 'master' version.  The
532`freebsd-to-rtems.py` script is used to transfer files between the two trees.
533In general terms, if you have modified managed FreeBSD sources, you will need
534to run the script in 'revert' or 'reverse' mode using the `-R` switch.  This
535will copy the source back to your local copy of the master FreeBSD source so
536you can run `git diff` against the upstream FreeBSD source.  If you want to
537transfer source files from the master FreeBSD source to the manged FreeBSD
538sources, then you must run the script in 'forward' mode (the default).
539
540=== Organization
541
542The top level directory contains a few directories and files. The following
543are important to understand
544
545* `freebsd-to-rtems.py` - script to convert to and free FreeBSD and RTEMS trees,
546* `create-kernel-namespace.sh` - script to create the kernel namespace header <machine/rtems-bsd-kernel-namespace.h,
547* `wscript` - automatically generated,
548* `freebsd/` - from FreeBSD by script,
549* `rtemsbsd/` - RTEMS specific implementations of FreeBSD kernel support routines,
550* `testsuite/` - RTEMS specific tests, and
551* `libbsd.txt` - documentation in Asciidoc.
552
553== Moving Code Between Managed and Master FreeBSD Source
554
555The script `freebsd-to-rtems.py` is used to copy code from FreeBSD to the
556rtems-libbsd tree and to reverse this process. This script attempts to
557automate this process as much as possible and performs some transformations
558on the FreeBSD code. Its command line arguments are shown below:
559
560----
561freebsd-to-rtems.py [args]
562  -?|-h|--help      print this and exit
563  -d|--dry-run      run program but no modifications
564  -D|--diff         provide diff of files between trees
565  -e|--early-exit   evaluate arguments, print results, and exit
566  -m|--makefile     Warning: depreciated and will be removed
567  -b|--buildscripts just generate the build scripts
568  -R|--reverse      default FreeBSD -> RTEMS, reverse that
569  -r|--rtems        RTEMS directory
570  -f|--freebsd      FreeBSD directory
571  -v|--verbose      enable verbose output mode
572----
573
574In its default mode of operation, freebsd-to-rtems.py is used to copy code
575from FreeBSD to the rtems-libbsd tree and perform transformations.  In forward
576mode, the script may be requested to just generate the Waf script.
577
578In "reverse mode", this script undoes those transformations and copies
579the source code back to the FreeBSD SVN tree. This allows us to do
580'svn diff', evaluate changes made by the RTEMS Project, and report changes
581back to FreeBSD upstream.
582
583In either mode, the script may be asked to perform a dry-run or be verbose.
584Also, in either mode, the script is also smart enough to avoid copying over
585files which have not changed. This means that the timestamps of files are
586not changed unless the contents change. The script will also report the
587number of files which changed. In verbose mode, the script will print
588the name of the files which are changed.
589
590To add or update files int the RTEMS FreeBSD tree first run the 'reverse mode'
591and move the current set of patches FreeBSD. The script may warn you if a file
592is not present at the destination for the direction. This can happen as files
593not avaliable at the FreeBSD snapshot point have been specially added to the
594RTEMS FreeBSD tree. Warnings can also appear if you have changed the list of
595files in libbsd.py. The reverse mode will result in the FreeBSD having
596uncommitted changes. You can ignore these. Once the reverse process has
597finished edit libbsd.py and add any new files then run the forwad mode to bring
598those files into the RTEMS FreeBSD tree.
599
600The following is an example forward run with no changes.
601
602----
603$ ~/newbsd/git/libbsd-8.2/freebsd-to-rtems.py \
604    -r /home/joel/newbsd/git/libbsd-8.2 \
605    -f /home/joel/newbsd/libbsd/freebsd-8.2 -v
606Verbose:                yes (1)
607Dry Run:                no
608Only Generate Makefile: no
609RTEMS Directory:        /home/joel/newbsd/git/libbsd-8.2
610FreeBSD Directory:      /home/joel/newbsd/libbsd/freebsd-8.2
611Direction:              forward
612Generating into /home/joel/newbsd/git/libbsd-8.2
6130 files were changed.
614----
615
616The script may also be used to generate a diff in either forward or reverse
617direction.
618
619You can add more than one verbose option (-v) to the command line and get more
620detail and debug level information from the command.
621
622== Initialization of the BSD Library
623
624The initialization of the BSD library is based on the FreeBSD SYSINIT(9)
625infrastructure.  The key to initializing a system is to ensure that the desired
626device drivers are explicitly pulled into the linked application.  This plus
627linking against the BSD library (`libbsd.a`) will pull in the necessary FreeBSD
628infrastructure.
629
630The FreeBSD kernel is not a library like the RTEMS kernel.  It is a bunch of
631object files linked together.  If we have a library, then creating the
632executable is simple.  We begin with a start symbol and recursively resolve all
633references.  With a bunch of object files linked together we need a different
634mechanism.  Most object files don't know each other.  Lets say we have a driver
635module.  The rest of the system has no references to this driver module.  The
636driver module needs a way to tell the rest of the system: Hey, kernel I am
637here, please use my services!
638
639This registration of independent components is performed by SYSINIT(9) and
640specializations:
641
642http://www.freebsd.org/cgi/man.cgi?query=SYSINIT
643
644The SYSINIT(9) uses some global data structures that are placed in a certain
645section.  In the linker command file we need this:
646
647-------------------------------------------------------------------------------
648.rtemsroset : {
649        KEEP (*(SORT(.rtemsroset.*)))
650}
651
652.rtemsrwset : {
653        KEEP (*(SORT(.rtemsrwset.*)))
654}
655-------------------------------------------------------------------------------
656
657This results for example in this executable layout:
658
659-------------------------------------------------------------------------------
660[...]
661 *(SORT(.rtemsroset.*))
662 .rtemsroset.bsd.modmetadata_set.begin
663                0x000000000025fe00        0x0 libbsd.a(rtems-bsd-init.o)
664                0x000000000025fe00                _bsd__start_set_modmetadata_set
665 .rtemsroset.bsd.modmetadata_set.content
666                0x000000000025fe00        0x8 libbsd.a(rtems-bsd-nexus.o)
667 .rtemsroset.bsd.modmetadata_set.content
668                0x000000000025fe08        0x4 libbsd.a(kern_module.o)
669[...]
670 .rtemsroset.bsd.modmetadata_set.content
671                0x000000000025fe68        0x4 libbsd.a(mii.o)
672 .rtemsroset.bsd.modmetadata_set.content
673                0x000000000025fe6c        0x4 libbsd.a(mii_bitbang.o)
674 .rtemsroset.bsd.modmetadata_set.end
675                0x000000000025fe70        0x0 libbsd.a(rtems-bsd-init.o)
676                0x000000000025fe70                _bsd__stop_set_modmetadata_set
677[...]
678.rtemsrwset     0x000000000030bad0      0x290
679 *(SORT(.rtemsrwset.*))
680 .rtemsrwset.bsd.sysinit_set.begin
681                0x000000000030bad0        0x0 libbsd.a(rtems-bsd-init.o)
682                0x000000000030bad0                _bsd__start_set_sysinit_set
683 .rtemsrwset.bsd.sysinit_set.content
684                0x000000000030bad0        0x4 libbsd.a(rtems-bsd-nexus.o)
685 .rtemsrwset.bsd.sysinit_set.content
686                0x000000000030bad4        0x8 libbsd.a(rtems-bsd-thread.o)
687 .rtemsrwset.bsd.sysinit_set.content
688                0x000000000030badc        0x4 libbsd.a(init_main.o)
689[...]
690 .rtemsrwset.bsd.sysinit_set.content
691                0x000000000030bd54        0x4 libbsd.a(frag6.o)
692 .rtemsrwset.bsd.sysinit_set.content
693                0x000000000030bd58        0x8 libbsd.a(uipc_accf.o)
694 .rtemsrwset.bsd.sysinit_set.end
695                0x000000000030bd60        0x0 libbsd.a(rtems-bsd-init.o)
696                0x000000000030bd60                _bsd__stop_set_sysinit_set
697[...]
698-------------------------------------------------------------------------------
699
700Here you can see, that some global data structures are collected into
701continuous memory areas.  This memory area can be identified by start and stop
702symbols.  This constructs a table of uniform items.
703
704The low level FreeBSD code calls at some time during the initialization the
705mi_startup() function (machine independent startup).  This function will sort
706the SYSINIT(9) set and call handler functions which perform further
707initialization.  The last step is the scheduler invocation.
708
709The SYSINIT(9) routines are run in mi_startup() which is called by
710rtems_bsd_initialize().
711
712This is also explained in "The Design and Implementation of the FreeBSD
713Operating System" section 14.3 "Kernel Initialization".
714
715In RTEMS we have a library and not a bunch of object files.  Thus we need a way
716to pull-in the desired services out of the libbsd.  Here the
717`rtems-bsd-sysinit.h` comes into play.  The SYSINIT(9) macros have been
718modified and extended for RTEMS in `<sys/kernel.h>`:
719
720-------------------------------------------------------------------------------
721#ifndef __rtems__
722#define C_SYSINIT(uniquifier, subsystem, order, func, ident)    \
723        static struct sysinit uniquifier ## _sys_init = {       \
724                subsystem,                                      \
725                order,                                          \
726                func,                                           \
727                (ident)                                         \
728        };                                                      \
729        DATA_SET(sysinit_set,uniquifier ## _sys_init)
730#else /* __rtems__ */
731#define SYSINIT_ENTRY_NAME(uniquifier)                          \
732        _bsd_ ## uniquifier ## _sys_init
733#define SYSINIT_REFERENCE_NAME(uniquifier)                      \
734        _bsd_ ## uniquifier ## _sys_init_ref
735#define C_SYSINIT(uniquifier, subsystem, order, func, ident)    \
736        struct sysinit SYSINIT_ENTRY_NAME(uniquifier) = {       \
737                subsystem,                                      \
738                order,                                          \
739                func,                                           \
740                (ident)                                         \
741        };                                                      \
742        RWDATA_SET(sysinit_set,SYSINIT_ENTRY_NAME(uniquifier))
743#define SYSINIT_REFERENCE(uniquifier)                           \
744        extern struct sysinit SYSINIT_ENTRY_NAME(uniquifier);   \
745        static struct sysinit const * const                     \
746        SYSINIT_REFERENCE_NAME(uniquifier) __used               \
747        = &SYSINIT_ENTRY_NAME(uniquifier)
748#define SYSINIT_MODULE_REFERENCE(mod)                           \
749        SYSINIT_REFERENCE(mod ## module)
750#define SYSINIT_DRIVER_REFERENCE(driver, bus)                   \
751        SYSINIT_MODULE_REFERENCE(driver ## _ ## bus)
752#define SYSINIT_DOMAIN_REFERENCE(dom)                           \
753        SYSINIT_REFERENCE(domain_add_ ## dom)
754#endif /* __rtems__ */
755-------------------------------------------------------------------------------
756
757Here you see that the SYSINIT(9) entries are no longer static.  The
758\*_REFERENCE() macros will create references to the corresponding modules which
759are later resolved by the linker.  The application has to provide an object
760file with references to all required FreeBSD modules.
761
762The FreeBSD device model is quite elaborated (with follow-ups):
763
764http://www.freebsd.org/cgi/man.cgi?query=driver
765
766The devices form a tree with the Nexus device at a high-level.  This Nexus
767device is architecture specific in FreeBSD.  In RTEMS we have our own Nexus
768device, see `rtemsbsd/bsp/bsp-bsd-nexus-devices.c`.
769
770=== SYSCTL_NODE Example
771
772During development, we had an undefined reference to
773_bsd_sysctl__net_children that we had trouble tracking down. Thanks to
774Chris Johns, we located it. He explained how to read SYSCTL_NODE
775definitions. This line from freebsd/netinet/in_proto.c is attempting
776to add the "inet" node to the parent node "_net".
777
778----
779SYSCTL_NODE(_net,      PF_INET,         inet,   CTLFLAG_RW, 0,
780        "Internet Family");
781----
782
783Our problem was that we could not find where _bsd_sysctl__net_children
784was defined. Chris suggested that when in doubt compile with -save-temps
785and look at the preprocessed .i files. But he did not need that. He
786explained that this the symbol name _bsd_sysctl__net_children was
787automatically generated by a SYSCTL_NODE as follows:
788
789* _bsd_ - added by RTEMS modifications to SYSCTL_NODE macro
790* sysctl_ - boilerplace added by SYSCTL_NODE macro
791* "" - empty string for parent node
792* net - name of SYSCTL_NODE
793* children - added by SYSCTL macros
794
795This was all generated by a support macro declaring the node as this:
796
797----
798struct sysctl_oid_list SYSCTL_NODE_CHILDREN(parent, name);
799----
800
801Given this information, we located this SYSCTL_NODE declaration in
802kern/kern_mib.c
803
804----
805SYSCTL_NODE(, CTL_KERN,   kern,   CTLFLAG_RW, 0,
806        "High kernel, proc, limits &c");
807----
808
809== Core FreeBSD APIs and RTEMS Replacements ==
810
811=== SX(9) (Shared/exclusive locks) ===
812
813http://www.freebsd.org/cgi/man.cgi?query=sx
814
815Binary semaphores (this neglects the ability to allow shared access).
816
817=== MUTEX(9) (Mutual exclusion) ===
818
819http://www.freebsd.org/cgi/man.cgi?query=mutex
820
821Binary semaphores (not recursive mutexes are not supported this way).
822
823=== RWLOCK(9) (Reader/writer lock) ===
824
825http://www.freebsd.org/cgi/man.cgi?query=rwlock
826
827POSIX r/w lock.
828
829=== RMLOCK(9) (Reader/writer lock optimized for mostly read access patterns) ===
830
831Note:  This object was implemented as a wrapper for RWLOCK in the rm_lock header file.
832
833http://www.freebsd.org/cgi/man.cgi?query=rmlock
834
835POSIX r/w lock.
836
837=== CONDVAR(9) (Condition variables) ===
838
839http://www.freebsd.org/cgi/man.cgi?query=condvar
840
841POSIX condition variables with modifications (hack).
842
843=== CALLOUT(9) (Timer functions) ===
844
845http://www.freebsd.org/cgi/man.cgi?query=callout
846
847Timer server.
848
849=== TASKQUEUE(9) (Asynchronous task execution) ===
850
851http://www.freebsd.org/cgi/man.cgi?query=taskqueue
852
853TBD.
854
855=== KTHREAD(9), KPROC(9) (Tasks) ===
856
857http://www.freebsd.org/cgi/man.cgi?query=kthread
858
859http://www.freebsd.org/cgi/man.cgi?query=kproc
860
861Tasks.
862
863=== ZONE(9) (Zone allocator) ===
864
865http://www.freebsd.org/cgi/man.cgi?query=zone
866
867TBD.
868
869=== devfs (Device file system) ===
870
871Dummy, IMFS or new implementation (currently dummy).
872
873=== psignal (Signals) ===
874
875TBD.  Seems to be not needed.
876
877=== poll, select ===
878
879TBD.  Seems to be not needed.
880
881=== RMAN(9) (Resource management) ===
882
883http://www.freebsd.org/cgi/man.cgi?query=rman
884
885TBD.  Seems to be not needed.
886
887=== DEVCLASS(9), DEVICE(9), DRIVER(9), MAKE_DEV(9) (Device management) ===
888
889http://www.freebsd.org/cgi/man.cgi?query=devclass
890
891http://www.freebsd.org/cgi/man.cgi?query=device
892
893http://www.freebsd.org/cgi/man.cgi?query=driver
894
895http://www.freebsd.org/cgi/man.cgi?query=make_dev
896
897Use FreeBSD implementation as far as possible.  FreeBSD has a nice API for
898dynamic device handling.  It may be interesting for RTEMS to use this API
899internally in the future.
900
901=== BUS_SPACE(9), BUS_DMA(9) (Bus and DMA access) ===
902
903http://www.freebsd.org/cgi/man.cgi?query=bus_space
904
905http://www.freebsd.org/cgi/man.cgi?query=bus_dma
906
907Likely BSP dependent.  A default implementation for memory mapped linear access
908is easy to provide.  The current heap implementation supports all properties
909demanded by bus_dma (including the boundary constraint).
910
911== RTEMS Replacements by File Description ==
912
913Note:  Files with a status of USB are used by the USB test and have at least
914been partially tested.  If they contain both USB and Nic, then they are used
915by both and MAY contain methods that have not been tested yet.  Files that
916are only used by the Nic test are the most suspect.
917
918----
919rtems-libbsd File:      rtems-bsd-assert.c
920FreeBSD File:           rtems-bsd-config.h redefines BSD_ASSERT.
921Description:            This file contains the support method rtems_bsd_assert_func().
922Status:                 USB, Nic
923
924rtems-libbsd File:      rtems-bsd-autoconf.c
925FreeBSD File:           FreeBSD has BSP specific autoconf.c
926Description:            This file contains configuration methods that are used to setup the system.
927Status:                 USB
928
929rtems-libbsd File:      rtems-bsd-bus-dma.c
930FreeBSD File:           FreeBSD has BSP specific busdma_machdep.c
931Description:
932Status:                 USB, Nic
933
934rtems-libbsd File:      rtems-bsd-bus-dma-mbuf.c
935FreeBSD File:           FreeBSD has BSP specific busdma_machdep.c
936Description:
937Status:                 Nic
938
939rtems-libbsd File:      rtems-bsd-callout.c
940FreeBSD File:           kern/kern_timeout.c
941Description:
942Status:                 USB, Nic
943
944rtems-libbsd File:      rtems-bsd-cam.c
945FreeBSD File:           cam/cam_sim.c
946Description:
947Status:                 USB
948
949rtems-libbsd File:      rtems-bsd-condvar.c
950FreeBSD File:           kern/kern_condvar.c
951Description:
952Status:                 USB
953
954rtems-libbsd File:      rtems-bsd-copyinout.c
955FreeBSD File:           bsp specific copyinout.c )
956Description:            Note: The FreeBSD file is split with some methods being in rtems-bsd-support
957Status:                 Nic
958
959rtems-libbsd File:      rtems-bsd-delay.c
960FreeBSD File:           bsp specific file with multiple names
961Description:
962Status:                 USB, Nic
963
964rtems-libbsd File:      rtems-bsd-descrip.c
965FreeBSD File:           kern/kern_descrip.c
966Description:
967Status:                 Nic
968
969rtems-libbsd File:      rtems-bsd-generic.c
970FreeBSD File:           kern/sys_generic.c
971Description:
972Status:                 Nic
973
974rtems-libbsd File:      rtems-bsd-init.c
975FreeBSD File:           N/A
976Description:
977Status:                 USB, Nic
978
979rtems-libbsd File:      rtems-bsd-init-with-irq.c
980FreeBSD File:           N/A
981Description:
982Status:                 USB, Nic
983
984rtems-libbsd File:      rtems-bsd-jail.c
985FreeBSD File:           kern/kern_jail.c
986Description:
987Status:                 USB, Nic
988
989rtems-libbsd File:      rtems-bsd-lock.c
990FreeBSD File:           kern/subr_lock.c
991Description:
992Status:                 USB, Nic
993
994rtems-libbsd File:      rtems-bsd-log.c
995FreeBSD File:           kern/subr_prf.c
996Description:
997Status:                 Nic
998
999rtems-libbsd File:      rtems-bsd-malloc.c
1000FreeBSD File:           kern/kern_malloc.c
1001Description:
1002Status:                 USB, Nic
1003
1004rtems-libbsd File:      rtems-bsd-mutex.c
1005FreeBSD File:           kern/kern_mutex.c
1006Description:
1007Status:                 USB, Nic
1008
1009rtems-libbsd File:      rtems-bsd-newproc.c
1010FreeBSD File:           N/A
1011Description:
1012Status:                 Nic
1013
1014rtems-libbsd File:      rtems-bsd-nexus.c
1015FreeBSD File:           bsp specific nexus.c
1016Description:
1017Status:                 USB
1018
1019rtems-libbsd File:      rtems-bsd-panic.c
1020FreeBSD File:           boot/common/panic.c
1021Description:
1022Status:                 USB, Nic
1023
1024rtems-libbsd File:      rtems-bsd-rwlock.c
1025FreeBSD File:           kern_rwlock.c
1026Description:
1027Status:                 USB, Nic
1028
1029rtems-libbsd File:      rtems-bsd-shell.c
1030FreeBSD File:           N/A
1031Description:
1032Status:                 USB
1033
1034rtems-libbsd File:      rtems-bsd-signal.c
1035FreeBSD File:           kern/kern_sig.c
1036Description:
1037Status:                 Nic
1038
1039rtems-libbsd File:      rtems-bsd-smp.c
1040FreeBSD File:           N/A
1041Description:
1042Status:                 Nic
1043
1044rtems-libbsd File:      rtems-bsd-support.c
1045FreeBSD File:           bsp specific copyinout.c
1046Description:            Note: the FreeBSD file is split with some methods being in rtems-bsd-copyinout.
1047Status:                 USB, Nic
1048
1049rtems-libbsd File:      rtems-bsd-sx.c
1050FreeBSD File:           kern/kern_sx.c
1051Description:            Status: USB, Nic
1052
1053rtems-libbsd File:      rtems-bsd-synch.c
1054FreeBSD File:           kern/kern_synch.c
1055Description:
1056Status:                 USB, Nic
1057
1058rtems-libbsd File:      rtems-bsd-syscalls.c
1059FreeBSD File:           User API for kern/uipc_syscalls.c
1060Description:
1061Status:                 Nic
1062
1063rtems-libbsd File:      rtems-bsd-sysctlbyname.c
1064FreeBSD File:           User API for sysctlbyname(3)
1065Description:
1066Status:
1067
1068rtems-libbsd File:      rtems-bsd-sysctl.c
1069FreeBSD File:           User API for sysctl(8)
1070Description:
1071Status:
1072
1073rtems-libbsd File:      rtems-bsd-sysctlnametomib.c
1074FreeBSD File:           User API for sysctlnametomib
1075Description:
1076Status:
1077
1078rtems-libbsd File:      rtems-bsd-taskqueue.c
1079FreeBSD File:           kern/subr_taskqueue.c
1080Description:
1081Status:                 Nic
1082
1083rtems-libbsd File:      rtems-bsd-thread.c
1084FreeBSD File:           kern/kern_kthread.c
1085Description:
1086Status:                 USB, Nic
1087
1088rtems-libbsd File:      rtems-bsd-timeout.c
1089FreeBSD File:           kern/kern_timeout.c
1090Description:
1091Status:                 Nic
1092
1093rtems-libbsd File:      rtems-bsd-timesupport.c
1094FreeBSD File:           kern/kern_clock.c
1095Description:
1096Status:                 Nic
1097
1098rtems-libbsd File:      rtems-bsd-vm_glue.c
1099FreeBSD File:           vm/vm_glue.c
1100Description:
1101Status:                 USB, Nic
1102----
1103
1104== Notes by File ==
1105
1106altq_subr.c - Arbitrary choices were made in this file that RTEMS would
1107not support tsc frequency change.  Additionally, the clock frequency
1108for machclk_freq is always measured for RTEMS.
1109
1110conf.h - In order to add make_dev and destroy_dev, variables in the cdev
1111structure that were not being used were conditionally compiled out. The
1112capability of supporting children did not appear to be needed and was
1113not implemented in the rtems version of these routines.
1114
1115== NICs Status ==
1116
1117----
1118Driver                  Symbol                          Status
1119======                  ======                          ======
1120RealTek                 _bsd_re_pcimodule_sys_init      Links
1121EtherExpress            _bsd_fxp_pcimodule_sys_init     Links
1122DEC tulip               _bsd_dc_pcimodule_sys_init      Links
1123Broadcom BCM57xxx       _bsd_bce_pcimodule_sys_init     Links
1124Broadcom BCM4401        _bsd_bfe_pcimodule_sys_init     Links
1125Broadcom BCM570x        _bsd_bge_pcimodule_sys_init     Needs Symbols (A)
1126E1000 IGB               _bsd_igb_pcimodule_sys_init     Links
1127E1000 EM                _bsd_em_pcimodule_sys_init      Links
1128Cadence                 ?                               Links, works.
1129----
1130
1131To add a NIC edit rtemsbsd/include/bsp/nexus-devices.h and add the driver
1132reference to the architecture and/or BSP. For example to add the RealTek driver
1133add:
1134
1135SYSINIT_DRIVER_REFERENCE(re, pci);
1136
1137and to add the MII PHY driver add:
1138
1139SYSINIT_DRIVER_REFERENCE(rge, miibus);
1140
1141The PC BSP has these entries.
1142
1143Symbols (A)
1144         pci_get_vpd_ident
1145
1146=== Cadence ===
1147
1148The cadence driver works on the Xilinx Zynq platform. The hardware checksum
1149support works on real hardware but does not seem to be supported on qemu
1150therefore the default state is to disable TXCSUM and RXCSUM and this can be
1151enabled from the shell with:
1152
1153  # ifconfig cgem0 rxcsum txcsum
1154
1155or with an ioctl call to the network interface driver with SIOCSIFCAP and the
1156mask IFCAP_TXCSUM and IFCAP_RXCSUM set.
1157
1158== Problems to report to FreeBSD ==
1159
1160The MMAP_NOT_AVAILABLE define is inverted on its usage.  When it is
1161defined the mmap method is called. Additionally, it is not used
1162thoroughly. It is not used in the unmap portion of the source.
1163The file rec_open.c uses the define MMAP_NOT_AVAILABLE to wrap
1164the call to mmap and file rec_close.c uses the munmap method.
Note: See TracBrowser for help on using the repository browser.