Commit graph

11425 commits

Author SHA1 Message Date
Jesus Sanchez-Palencia 8ae392e66f x86: Reduce .eh_frame section size
When generating binaries, gcc will always add information of what it
calls "the exception handler framework" into its own section: .eh_frame.
This section is based on the DWARF format's call frame information (CFI) [1]
and holds information that can be useful for debuggers but also for language
constructs that relies on always having stack unwinding information (i.e. exceptions).
Such constructs, however, are pretty much useless for the C language and are
mainly just used on C++. Furthermore, this section is one of the loadable sections
of a binary, meaning it will take extra space on flash.

When .eh_frame is not present, debuggers can still get the exact same information
they need for unwinding a stack frame and for restoring registers thanks to yet
another section: .debug_frame. This section is generated by '-g' gcc option and
friends. It is actually defined by DWARF and, as opposed to .eh_frame, is not a
loadable section. In other words, it is 'strippable' while .eh_frame is not.

Since all we need is the debug information we can get from .debug_frame, we can
disable the generation of these large and unused information tables by using gcc's
'-fno-asynchronous-unwind-tables'. The .eh_frame section stays around but the code
size issue is heavily tackled. This is the same approach taken on other projects
that target small code size generation [2] [3].

Pratically speaking, on a DEBUG build of the all-timers appplication, before this
patch we had:
   text    data     bss     dec     hex filename
  21319    1188   12952   35459    8a83 all-timers.galileo

And now, after this patch:
   text    data     bss     dec     hex filename
  16347    1188   12952   30487    7717 all-timers.galileo

This means a ~5Kb reduction on the loadable text segment (.text + .rodata + .eh_frame).

The flag is applied regardless of build type, DEBUG or RELEASE, since it benefits both.
Note that when release builds apply --gc-sections, they will remove .eh_frame section entirely.

[1] http://comments.gmane.org/gmane.comp.standards.dwarf/222
[2] 0d74ad383b
[3] http://git.musl-libc.org/cgit/musl/commit/?id=b439c051c7eee4eb4b93fc382f993aa6305ce530
[4] https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-Core-generic/LSB-Core-generic/ehframechpt.html

Signed-off-by: Jesus Sanchez-Palencia <jesus.sanchez-palencia@intel.com>
2015-12-21 08:06:14 -02:00
Michael LeMay 0dcd5e9b5a x86: Revise CFLAGS and LDFLAGS for LLVM Clang compatibility
This patch slightly revises CFLAGS and LDFLAGS to specify the
optimization and debugging options and linker script in a way that is
compatible with using Clang as the C compiler and to invoke the linker
(i.e. CC = clang and LD = clang).
2015-12-21 08:06:14 -02:00
Michael LeMay 128d9f3566 x86: Revise SET_INTERRUPT_HANDLER to avoid using inline assembly feature that does not work with LLVM Clang
The SET_INTERRUPT_HANDLER macro in interrupt.h used an inline assembly
feature to cause GCC to generate a unique number for a trampoline
label.  Clang compiled the code using that feature without generating
any compile-time errors, but it always generated the number 0,
resulting in all interrupt trampolines having the same label names.
This patch replaces the usage of that feature with local labels, which
are supported by both GCC and Clang.  See
https://sourceware.org/binutils/docs/as/Symbol-Names.html for an
explanation of local labels.
2015-12-21 08:06:14 -02:00
Michael LeMay f9072c166b x86: Add missing clobber list in interrupt.h
The SET_INTERRUPT_HANDLER macro defines and registers an interrupt
handler.  It outputs a trampoline for the interrupt handler using a
block of inline assembly, and the address of that trampoline is what
is actually placed in the IDT.  That trampoline invokes the main body
of the interrupt handler.

This patch adds a missing clobber list to the inline assembly block.
It simply lists the caller-saved registers defined by the cdecl
calling convention: EAX, ECX, and EDX.  This is necessary, because the
inline assembly block invokes idt_set_intr_gate_desc using a call
instruction at the time the function containing the
SET_INTERRUPT_HANDLER instance is executed.  The
idt_set_intr_gate_desc function is free to clobber EAX, ECX, and EDX
according to cdecl.  A Clang-generated implementation of
idt_set_intr_gate_desc did in fact clobber those registers, resulting
in incorrect operation of the code following an instance of
SET_INTERRUPT_HANDLER.  The change in this patch informs the compiler
that those registers may be clobbered so that it can adjust the code
that it outputs around the inline assembly block accordingly.
2015-12-21 08:06:14 -02:00
Michael LeMay 17b855aac9 galileo: Replace non-halting core implementation of assert with the halting one from newlib
This patch modifies the include order to include headers from newlib
ahead of those from the core of Contiki.  The only header file names
that are common between Contiki and newlib are assert.h and config.h,
but the config.h files in Contiki are only located in ports for other
CPUs so they are irrelevant to this patch.  The motivation for this
patch is to cause files that include assert.h to include the one from
newlib that halts when an assertion fails.  The assert implementation
in the core of Contiki does not halt when an assertion fails.

This patch also adds newlib syscall stubs that are required by the
newlib assert implementation and the _exit syscall function that halts
the system.

Finally, this patch updates some other newlib syscall stubs to
properly indicate their status as unsupported syscalls.
2015-12-21 08:06:14 -02:00
Michael LeMay 06e25c487a galileo: Correct README.md to explain how to verify boot with UART
This patch revises README.md to mention the UART support introduced by
earlier patches in the section about verifying that Contiki is
running.  It also revises the serial console setup instructions to
focus on the more thoroughly tested option.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia bdcf58033a x86: Group sections for QuarkX1000
Child sections can be created depending on the CFLAGS used when building Contiki.
For instance, if built with -ffunction-sections or -fdata-sections would create
one section per function/variable and -Os sometimes split part of .text into one
child section.

This can also be an issue if we start linking with external libraries that are
built in such a way. Since we can't foresee how the port is going to be used,
we should make sure it is ready for the cases above.

This patch fixes this by correctly grouping child sections into their parent
sections.

Patch developed while investigating a bug with José Souza (jose.souza@intel.com).
2015-12-21 08:06:14 -02:00
Andre Guedes e41bed319f galileo: Revise stdlib static linking
This patch does some refactoring so we are able to statically link
against standard libraries in a more usual way.
2015-12-21 08:06:14 -02:00
Andre Guedes 7043aa41b3 galileo: Fix newlib-syscalls issue in Makefile
This patch removes 'newlib-syscalls.c' from CONTIKI_SOURCEFILES variable
and appends it to PROJECT_SOURCEFILES. This way the buildsystem will
automatically consider the newlib-syscalls object code during linking
time.
2015-12-21 08:06:14 -02:00
Andre Guedes 8a0bc49433 x86: Improve debugging experience
This patch appends some gcc options to CFLAGS when building the default
image in order to improve the debugging experience on GDB.

We use the '-ggdb' option which produces debugging information used by
GDB (including GDB extensions) with level 3 which includes preprocessor
macros information. We also use '-Og' which enables optimizations that
do not interfere with debugging. According to gcc manpage, it should be
the optimization level of choice for the standard edit-compile-debug
cycle, offering a reasonable level of optimization while maintaining
fast compilation and a good debugging experience.

Also, this patch removes the '-g' option from the default CFLAGS because
there is no point in using it when BUILD_RELEASE=1.

As expected, the overall ELF image increases (due to -ggdb3 option) while
the .text section is reduced (due to -Og). For the sake of comparison,
below follows the output of 'size'.

Before patch:
$ size -A hello-world.galileo
hello-world.galileo  :
section           size      addr
.text            13766   1048576
.rodata            241   1064960
.eh_frame         5160   1065204
.eh_frame_hdr     1212   1070364
.data             1188   1073152
.bss             12808   1077248
.debug_info      14351         0
.debug_abbrev     6281         0
.debug_aranges     768         0
.debug_line       6443         0
.debug_str        4805         0
.comment            17         0
.note               40         0
.debug_ranges       24         0
Total            67104

After patch:
$ size -A hello-world.galileo
hello-world.galileo  :
section            size      addr
.text             11718   1048576
.rodata             249   1060864
.eh_frame          5496   1061116
.eh_frame_hdr      1204   1066612
.data              1156   1069056
.bss              12808   1073152
.debug_info       16727         0
.debug_abbrev      7254         0
.debug_loc         2083         0
.debug_aranges      768         0
.debug_macro      17273         0
.debug_line       13433         0
.debug_str        42192         0
.comment             17         0
.note                40         0
Total            132418
2015-12-21 08:06:14 -02:00
Andre Guedes c9020d95e7 x86: Build release image
This patch adds support for building release images. The main difference
between release images and default images is that the former is optimized
for size while the latter is "optimized" for debugging. To build a release
image, the BUILD_RELEASE variable should be set to 1. For instance, the
following command build a release image from the hello-world application:
$ cd examples/hello-world && make TARGET=galileo BUILD_RELEASE=1

To optimize for size we use the '-Os' option from gcc. This option also
enables the strict aliasing optimization. This generates lots of warning
messages since we use the '-Wall' option and lots of code in core/net/
break the strict-aliasing rules. Some test have shown that the strict
aliasing optimization it not taking effect in the final binary. For that
reasons, this patch manually disables the optimization. Also, the release
image is stripped.

For the sake of comparison, below follows the output from 'wc' and 'size'
for both debugging (default) and release images.

Default image:
$  wc -c hello-world.galileo
71112 hello-world.galileo
$ size hello-world.galileo
   text    data     bss     dec     hex filename
  20379    1188   12808   34375    8647 hello-world.galileo

Release image:
$ wc -c hello-world.galileo
26320 hello-world.galileo
$ size hello-world.galileo
   text    data     bss     dec     hex filename
  18146    1156   12808   32110    7d6e hello-world.galileo
2015-12-21 08:06:14 -02:00
Andre Guedes 6c9ab4eb6c galileo: Remove unneeded syscall stubs
This patch removes _kill_r and _getpid_r stubs since they are not
required by newlib at this moment. These stubs should not be in
the commit that introduced the initial newlib-syscalls.c file.
2015-12-21 08:06:14 -02:00
Andre Guedes dd540e9a21 gitignore: Add platform/galileo/bsp/libc/Makefile.libc 2015-12-21 08:06:14 -02:00
Andre Guedes d3d2b51fa2 galileo: Initial stdio support
This patch introduces the initial support for stdio library in Galileo
platform. For now, only standard output and error are supported. Both
streams use the UART1 device.

Newlib doesn't call open() for stdin, stdout, and stderr which means
that the _write_r call is the first activity the stub will see on
those streams. For that reason, we initialize the UART1 device in
Galileo's platform main() function instead of in open() system call.
2015-12-21 08:06:14 -02:00
Andre Guedes ee82304211 galileo: Implement _sbrk_r syscall
This patch implements the _sbrk_r() system call for Galileo platform. This
system call is required by newlib's malloc() implementation. Next patch
will introduce the initial support for stdio library which requires a
working malloc() function for proper operation.

We are not sure about the heap size we should use. Preliminary tests
have shown that stdio library requests 1032 bytes heap. So, as an initial
guess, a 2Kb heap size should be enough for now.
2015-12-21 08:06:14 -02:00
Michael LeMay 15f947fe40 x86: Add Intel Quark X1000 UART support.
This patch adds a driver that wraps the generic 16X50 UART driver with
specific support for the two Intel Quark X1000 built-in UARTs.
2015-12-21 08:06:14 -02:00
Michael LeMay 6acdf50262 x86: Add driver for MMIO-accessible 16X50 UART
This patch adds a driver for an MMIO-accessible 16X50 UART.  It
assumes that the boot firmware assigned an MMIO range to the UART.  It
operates in polled mode with FIFOs enabled.
2015-12-21 08:06:14 -02:00
Michael LeMay c5f9cefac7 x86: Add generic driver structure and associated initialization code
This patch adds a generic device driver structure with a field for
referencing an MMIO range.  It also provides a structure
initialization procedure that initializes the MMIO range field with
the value read from the PCI BAR0 register for a device.
2015-12-21 08:06:14 -02:00
Michael LeMay 2dccb55e15 x86: Add PCI support
This patch adds the pci.c and pci.h files, which support access to PCI
configuration registers through a function interface.  It defines the
PCI configuration register access I/O port addresses and the
pci_config_addr union and structure to assist in specifying addresses
of PCI configuration registers.  It also defines the PCI configuration
register identifier for PCI BAR0.

This patch also adds wrappers for 32-bit 'in' and 'out' port I/O
instructions.  They were placed in helpers.S, since they may be useful
to other modules besides just the PCI support module.
2015-12-21 08:06:14 -02:00
Michael LeMay 62fc195d0f x86: Refactor GDT initialization code
This patch refactors the GDT initialization code in more of a
self-documenting style.
2015-12-21 08:06:14 -02:00
Michael LeMay 9b6b5ce5b0 galileo: Instruct developer to build C library prior to building Contiki
This patch enhances build_newlib.sh to create Makefile.libc so that
the main Galileo makefile can attempt to include Makefile.libc and
instruct the developer to run build_newlib.sh first if the definition
within Makefile.libc is not detected.
2015-12-21 08:06:14 -02:00
Michael LeMay b2e4786187 galileo: Stylistic changes to make build_newlib.sh less repetitive
This script defines new variables to represent common paths and
filenames.  It does not introduce any functional changes.
2015-12-21 08:06:14 -02:00
Michael LeMay c8cdc0c157 galileo: Check for errors after downloading newlib tarball
Repeat the file existence and MD5 checks in build_newlib.sh after the
newlib tarball is downloaded and exit if either of the checks fail.
2015-12-21 08:06:14 -02:00
Andre Guedes 96e50ec2c4 galileo: Fix Makefile.galileo
This patch moves the compiler and linking options related to QuarkX1000
SoC to Makefile.x86_quarkX1000 since it is more suitable. For instance,
'-m32' should be used in any platform based on QuarkX1000, not only
Galileo. The same rationale applies for the others options (e.g. -march,
mtune).
2015-12-21 08:06:14 -02:00
Andre Guedes b697646b11 x86: Cleanup Makefile.x86_common
This patch does several cleanups in Makefile.x86_common file. The
changes are described above.

1) The CFLAGNO variable was removed since it is used only to assign
   the CFLAGS variable. Also, CFLAGNO is not used outside Makefile.x86_
   common.

2) The "-I/usr/local/include" option was removed since we provide manually
   the include path from newlib in the bsp/ directory.

3) We only support building x86-based platforms on Linux so there is no
   point in setting LDFLAGS conditionally.

4) The '-export-dynamic' option was removed from LDFLAGS since we are not
    creating a dynamically linked executable.

5) Makefile.x86_quarkX1000 is the only one that includes Makefile.x86_
   common. Since it doesn't use the custom rules from Makefile.x86_
   common we remove them.
2015-12-21 08:06:14 -02:00
Michael LeMay 2b5efdfbe7 galileo: Revise C compiler flags for consistency.
The CFLAGS setting used for the newlib build process includes
"-mtune=i586" as does the ASFLAGS setting used for the Contiki build
process.  However, the CFLAGS setting used for the Contiki build
process did not include that flag.  This patch adds it for
consistency.
2015-12-21 08:06:14 -02:00
Michael LeMay b79fcaa7d8 galileo: Override Ubuntu's default GCC flags to avoid link errors
Ubuntu enables GCC's stack protector by default (see
https://wiki.ubuntu.com/Security/Features).  This causes link errors
like the following:

 ...undefined reference to `__stack_chk_fail'

To avoid these errors, this patch adds the "-fno-stack-protector" flag
to both the CFLAGS used by the Contiki build process and the CFLAGS
used by the newlib build process.
2015-12-21 08:06:14 -02:00
Andre Guedes 13bbe8a5b5 x86: Don't generate .note.gnu.build-id section
This patch adds "--build-id=none" to default LDFLAGS so
.note.gnu.build-id section is not generated. This section
contains unique identification for the built files what is
not important to us (at least at this moment).

This change simplifies all linker scripts for SoCs based on x86
(at this moment we only have Quark X1000) since we don't have to
care about it anymore.
2015-12-21 08:06:14 -02:00
Andre Guedes cb0510ebcf x86: Disable NMI while initializing RTC
According to [1], we should disable non-maskable and maskable interrupts
while initializing RTC. Otherwise, the RTC may be left in an undefined
state (non-functional) if an interrupt occurs. Currently, maskable
interrupts are already disabled, but NMI is not.

This patch adds helpers APIs to enable/disable non-maskable interrupts
(NMI) and changes rtc_init() to disable NMI while initializing the RTC.

NMI enable/disable code is legacy-PC specific therefore it was put in
driver/legacy_pc/ directory.

Regarding the RTC initialization changes, just calling nmi_disable() and
nmi_enable is not enough since NMI and RTC share the same IO port. So We
should also set the NMI_ENABLE bit while selecting the RTC_INDEX.

Additionally, the nmi_disable() call is not strictly required since we
set the NMI_ENABLE bit while selecting the RTC_INDEX. However, to make
clear hat we are disabling NMI and to improve readability (by matching
NMI disable/enable), the nmi_disable() call was purposely used.

[1] http://wiki.osdev.org/RTC
2015-12-21 08:06:14 -02:00
Andre Guedes a8849b2909 x86: Move bootstrap code and linker script to cpu/x86
This is a refactoring patch, no functionality is changed. It moves
loader.S and galileo.ld from platform/galileo/ to cpu/x86/ directory
since they seem to be more SoC-specific than platform-specific.

It also renames galileo.ld to quarkX1000.ld since it can be used by
any platform based on Quark X1000 SoC, not only Galileo.

Furthermore, this patch also renames loader.S to bootstrap_quarkX1000.S
since it is pretty much a bootstrap code to any platform based on Quark
X1000 SoC.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia 80fe1de0b1 x86: Isolate SoC specific cpu_init code
This commit turns cpu_init() into a SoC-agnostic function by
removing any SoC specific calls and isolating them into their
own SoC implementation.

We start this by isolating all IRQs initialization code from
the legacy-pc target, pic_init() and spurious IRQ7 registration,
into a new interface: irq_init() from irq.h. Future SoCs will have
to provide their own implementation of this interface.

This model is to be followed by future initialization code that
we may need to add and which is not common to all x86 SoCs.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia e4bc1a1e8c x86: Add init folder and move code accordingly
The x86/init/common/ folder holds all cpu initialization
code - idt and gdt setup, interrupts and cpu initialization.

On this folder will also sit any SoC specific implementation of
the functions called from cpu_init().
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia b2fa72bb98 x86: Break Makefile.x86 into common and pc specific ones
Now the cpu/x86/ provides a Makefile.x86_common and a
Makefile.x86_pc. The former includes the common Makefile
and adds legacy pc specific implementations (currently,
drivers only) into the building context, while the latter
has everything that defines the bootstrap of a x86 CPU.

This commit also fixes platform/galileo/ so it includes the
correct makefile - Makefile.x86_quarkX1000. Galileo uses
a Quark X1000 SoC which is not an IBM Generic PC-like CPU,
but it does provide most of a PCs peripherals through
its "Legacy Bridge". Thus, it makes sense that QuarkX1000's
Makefile includes code from the legacy_pc x86 cpu.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia 23e8090257 x86: Move available drivers into drivers/legacy_pc/
All drivers implemented so far are for chips which are only available
on legacy x86 PCs. This commit moves them into a more appropriate folder,
also making the cpu/x86/drivers/ folder ready for other x86 based SoCs.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia 9d3b9cadc4 galileo: Concentrate core implementations in platform/galileo/core/
Currently, it is common to see Contiki's core/ interfaces implementations
spread in both cpu/ and platform/. We here take one step further starting
an effort to centralize all of these in platform's code instead.

This commit starts this by adding platform/galileo/core/ and its sys/
subfolder, adding a stubbed mtarch.h and moving clock and rtimer
implementations to this new folder. From now on we should concentrate
implementation from Contiki's core/ interfaces into the appropriate
subfolder in platform/galileo/core/.

Note that this is not the current fashion followed on other platforms
and cpus folders, as most of them add the core interface implementation
into its subfolder directly. For instance, on CC2538DK,
core/dev/button-sensor.h is implemented in platform/cc2538dk/dev/
directly, while on Galileo it would sit at platform/galileo/core/dev/.
We believe ours is a better approach to organize and escalate a
platform's code base.

We also remove previous x86 mtarch.h and mtarch.c since they weren't used
at all - both native and cooja platforms have their own mtarch
implementations.
2015-12-21 08:06:14 -02:00
Andre Guedes 568f565b3d galileo: Update README.md file
This patch updates the README.md file, including information about the
current device drivers implementations as well as the Contiki APIs
supported.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia 3b01e04379 x86: Add a fake IRQ7 handler to avoid spurious interrupts
The 8259a PIC has a well known problem of generating flaky
IRQ7 interrupts. The correct solution is to always check
if an IRQ7 interrupt is real or not by probing the PIC's ISR
register. This check is only mandatory if the IRQ7 is actually
being used by the system. More importantly, the handler should
NEVER send and EOI if the interrupt was spurious.

This patch addresses this issue by implementing a fake empty
handler for this IRQ and, as stated, NOT sending the EOI.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia afd9b5b0b7 x86: Add APIs to 8259 PIC driver
This commit implements pic_eoi(int irq) and a helper macro PIC_INT(irq).
This first checks which PICs should be 'acked' given an IRQ number, while
the macro returns the actual system interrupt number for the IRQ according
to the offset used on the PIC initialization.
2015-12-21 08:06:14 -02:00
Andre Guedes 826ff7cb29 x86: Add pic_unmask_irq() helper
This patch implements the pic_unmask_irq() helper and uses it where
applicable. This function zeros the corresponding bit from the IRQ
number in IMR register.

This patch doesn't implement the pic_mask_irq() helper since it is not
useful at this moment.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia b8056b9c97 examples: Add all-timers example
This commit adds a very simple example which is useful to verify
that all timers APIs are working. There are 3 protothreads running,
the first process tests etimer, timer and stimer APIs, the second
process tests the ctimer APIs, and the third one tests the rtimer
APIs.
2015-12-21 08:06:14 -02:00
Andre Guedes e4ff61ff6c galileo: Support for rtimer library
This patch adds support for rtimer library on Galileo's platform.

We use the PIT to implement the rtimer platform dependent
functionalities. We chose the PIT for mainly two reason: I) its
configuration is very simple II) it has a high frequency which
provides us a good clock resolution (requirement from rtimer
library).

Since we keep track of the number of ticks in software, we define
rtimer_clock_t type as uint64_t. This gives us a good amount of time
til the variable overflows. For instance, a 32-bit type would overflow
in about one hour for high clock resolution (~ 1us).

The rtimer clock frequency (RTIMER_ARCH_SECOND) is setup to 1 kHz.
There is no technical matter regarding this value. It is just an
initial guess.

Just for the record, we might want to use HPET in future to
implement the rtimer library since it seems to be more appropriate.
The reason why we don't use it at this moment is that, in order to
configure it, we need support for ACPI 2.0 which we don't. Once we
have use-cases for the rtimer library we'll probably replace PIT
by HPET or any other timer more suitable for the job.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia d70f67cd60 galileo: Add PIT driver
This patch adds a driver for the 8254 Programmable Interrupt Timer (PIT).
The driver introduced by this patch programs the PIT to generate interrupt
periodically. The interrupt frequency can be configured by the user.

On each PIT interrupt, a callback configured by the user is called. As
expected, that callback is executed in interrupt context so the user
should be aware of what it is not supposed to do (e.g. to call blocking
functions).

Issues marked as FIXME are all related to missing APIs on the PIC driver
so they will be addressed by a future commit.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia 7c871871de galileo: Add support for Etimer and Ctimer libraries
This patch adds support for the Etimer and Ctimer libraries. To support
the Etimer library, we should poll the etimer process every time the
system clock is updated. To do this more efficiently, by taking advantage
of etimer_next_expiration_time() API, we poll the etimer process only
when an 'Event Timer' has expired.

We don't need any platform specific support in order to enable the Ctimer
library since it relies completely on Etimer.

The others timer libraries (Timer and Stime) don't required any specific
platform support as well since they rely on the system Clock module only.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia eafcba5e7a galileo: Add support for Clock module
This patch adds support for Contiki's clock module. All functions from
core/sys/clock.h are implemented, except clock_set_seconds() and clock_
delay_usec(). The CLOCK_CONF_SECOND macro is set to 128. This value
seems to be good enough since several platforms used it. Finally, we
use the RTC driver to track the number of ticks from the system clock.
2015-12-21 08:06:14 -02:00
Andre Guedes 5f47bafc6a x86: Add Real-Time Clock Driver
This patch adds a driver for Real-Time Clock (RTC). The RTC timer is
suitable to implement some operating system features such as the
system clock. Actually, the RTC will be used to implement the system
clock in galileo platform.

The driver introduced by this patch programs the RTC to generate
interrupt periodically. The interrupt frequency can be configured by the
user. On each RTC interrupt, a callback configured by the user is called.
As expected, that callback is executed in interrupt context so the user
should be aware of what it is not supposed to do (e.g. to call blocking
functions).

This patch also adds the inb() helper function to helpers.h. The helpers
is a wrapper for assembly 'in' instruction.
2015-12-21 08:06:14 -02:00
Jesus Sanchez-Palencia 11098501d8 x86: Initialize the 8259 PIC
The Programmable Interrupt Controller is a chip responsible for
translating hardware interrupts to system interrupts. When it
receives an Interrupt Request (IRQ), it triggers the appropriate
interrupt line reaching the appropriate IDT gate, following a
previously setup offset.

There are 2 daisy-chained PICs. PIC1 handles IRQs 0-7 and PIC2
handles IRQs 8-15. If no vector offset is set, an IRQ0, for instance,
would trigger the interrupt 0, clashing with the "Division by zero exception"
handler. Thus the IRQs must be remapped.

This patch implements the PICs initialization through their 4
Initialization Command Words (ICWs) in a very "canonical" way:
- ICW1: the initializing command;
- ICW2: the vector offset for the PIC1 and PIC2 (we add an offset of 32 positions);
- ICW3: the inter-PICs wiring setup (we connect PIC2 to PIC1's IRQ2);
- ICW4: extra systems information (we set PIC1 as Master and PIC2 as slave).

It then masks the Interrupt Mask Register, blocking all IRQs but #2 initially.
These must be unmasked on demand. The IMR is 8-bits long, so setting the n^th bit to 1
would DISABLE the IRQ n while setting it to 0 would ENABLE IRQ n.

As stated, this is an implementation of the legacy 8259 PIC. More
investigation is needed so we decide if it is enough or if we need
the (newer) APIC implementation instead.

This patch also adds the outb() helper function to helpers.h. The helpers
is a wrapper for assembly 'out' instruction.

Finally, since we now properly support hardware interrupts, this patch
also enables IRQs in platform main().

More information:
- Quark X1000 Datasheet, section 21.12, page 898.
- http://wiki.osdev.org/8259_PIC
- http://stanislavs.org/helppc/8259.html
2015-12-21 08:06:14 -02:00
Andre Guedes 604538ed62 x86: Set interrupt handler for Double Fault exception
This patch sets an interrupt handler for Double Fault exception during
CPU initialization. In case such exception is raised, we halt the system.
This way, we avoid the system to triple fault (due to an unhandled
interrupt for instance), leaving no trace about what cause the triple
fault.
2015-12-21 08:06:14 -02:00
Andre Guedes e28f400e0c x86: Introduce interrupt.h
This patch introduces the interrupt.h header file which provides some
helper macros to set a interrupt handler and disable/enable maskable
hardware interrupts.

Since there is no easy way to write an Interrupt Service Routines
(ISR) in C (for further information on this, see [1]), we introduce
the SET_INTERRUPT_HANDLER helper macro.

The macro does two things:
1) Defines an assembly trampolin to a C function that will, indeed,
   handle the interrupt.
2) Sets the corresponding interrupt gate descriptor in IDT.

The macro usage is pretty straightforward. The macro is defined as
SET_INTERRUPT_HANDLER(num, has_error_code, handler) where:
@num:             Interrupt number (0-255)
@has_error_code:  0 if processor doesn't push error code onto the
                  stack. Otherwise, set this argument to 1.
@handler:         Pointer to function that should be called once the
                  interrupt is raised. In case has_error_code == 0
                  the function prototype should be the following:
                  void handler(void)
                  Otherwise, it should be:
                  void handler(struct interrupt_context context)

For instance, let's say we want to set a handler for a device interrupt
(for example, interrupt number 101). Remember, hardware interrupts don't
have error code. So we should have something like this:

void interrupt_handler(void)
{
        /* Handling code here */
}

void my_device_init(void)
{
        ...

        SET_INTERRUPT_HANDLER(101, 0, interrupt_handler);

        ...
}

Now, let's say we want to set an interrupt handler for Page Fault
(interrupt number 14). Some exceptions, such as Page Fault, pushes an
error code onto the stack and may require registers values in order
to be properly be handled. Thus, the code should look like this:

void pagefault_handler(struct interrupt_context context)
{
        /* Handling code here */
}

void init_memory(void)
{
        ...

        SET_INTERRUPT_HANDLER(14, 1, pagefault_handler);

        ...
}

For further information about exceptions and error code, refer to Intel
Combined Manual, Vol. 3, Sections 6.3 and 6.13.

Finally, we don't define any API to unregister interrupt handlers since
we believe that it wouldn't be useful at all, at least at this moment.
Considering Contiki's context, interrupt handler registration is pretty
"static" and defined at compile-time by platform code (or the device
drivers used by the platform).

[1] http://wiki.osdev.org/Interrupt_Service_Routines
2015-12-21 08:06:14 -02:00
Andre Guedes f6644d9208 x86: CPU Initialization
This patch defines the cpu_init() function which should encapsulate
all code related to x86 CPU initialization. For now, this function
initializes GDT and IDT.
2015-12-21 08:06:14 -02:00
Andre Guedes 13d92cf67a x86: Initialize Interrupt Descriptor Table
This patch adds code to handle Interrupt Descriptor Table (IDT)
initialization. The IDT is initialized with null descriptors
therefore any interrupt at this point will cause a triple fault.
The IDT initialization is part of x86 CPU initialization.

Strictly speaking, there is no need to use attribute packed in struct
intr_gate_desc however we use it for readability reasons.
2015-12-21 08:06:14 -02:00