Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a sparc-unknown-none-elf target. #113535

Closed

Conversation

jonathanpallant
Copy link
Contributor

sparc-unknown-none-elf

Tier: 3

Rust for bare-metal 32-bit SPARC V7 and V8 systems, e.g. the Gaisler LEON3.

Target maintainers

Requirements

Does the target support host tools, or only cross-compilation?

Only cross-compilation.

Does the target support std, or alloc (either with a default allocator, or if the user supplies an allocator)?

Only tested with libcore but I see no reason why you couldn't also support liballoc.

Document the expectations of binaries built for the target. Do they assume
specific minimum features beyond the baseline of the CPU/environment/etc? What
version of the OS or environment do they expect?

Tested by linking with a standard SPARC bare-metal toolchain - specifically I used the BCC2 toolchain from Gaisler (both GCC and clang variants, both pre-compiled for x64 Linux and compiling my own SPARC GCC from source to run on aarch64-apple-darwin).

The target is set to use the lowest-common-denominator SPARC V7 architecture (yes, they started at V7 - see Wikipedia).

Are there notable #[target_feature(...)] or -C target-feature= values that
programs may wish to use?

-Ctarget-cpu=v8 adds the instructions added in V8.

-Ctarget-cpu=leon3 adds the V8 instructions and sets up scheduling to suit the Gaisler LEON3.

What calling convention does extern "C" use on the target?

I believe this is defined by the SPARC architecture reference manuals and V7, V8 and V9 are all compatible.

What format do binaries use by default? ELF, PE, something else?

ELF

Building the target

If Rust doesn't build the target by default, how can users build it? Can users
just add it to the target list in config.toml?

Yes. I did:

target = ["aarch64-apple-darwin", "sparc-unknown-none-elf"]

Building Rust programs

Rust does not yet ship pre-compiled artifacts for this target. To compile for
this target, you will either need to build Rust with the target enabled (see
"Building the target" above), or build your own copy of core by using
build-std or similar.

Correct.

Testing

Does the target support running binaries, or do binaries have varying
expectations that prevent having a standard way to run them?

No - it's a bare metal platform.

If users can run binaries, can they do so in some common emulator, or do they need native
hardware?

But if you use BCC2 as the linker, you get default memory map suitable for the LEON3, and a default BSP for the LEON3, and so you can run the binaries in the tsim-leon3 simulator from Gaisler.

$ cat .cargo/config.toml | grep runner
runner = "tsim-leon3 -c sim-commands.txt"
$ cat sim-commands.txt
run
quit
$ cargo +sparcrust run --targe=sparc-unknown-none-elf
   Compiling sparc-demo-rust v0.1.0 (/work/sparc-demo-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 3.44s
     Running `tsim-leon3 -c sim-commands.txt target/sparc-unknown-none-elf/debug/sparc-demo-rust`

 TSIM3 LEON3 SPARC simulator, version 3.1.9 (evaluation version)

 Copyright (C) 2023, Frontgrade Gaisler - all rights reserved.
 This software may only be used with a valid license.
 For latest updates, go to https://www.gaisler.com/
 Comments or bug-reports to [email protected]

 This TSIM evaluation version will expire 2023-11-28

Number of CPUs: 2
system frequency: 50.000 MHz
icache: 1 * 4 KiB, 16 bytes/line (4 KiB total)
dcache: 1 * 4 KiB, 16 bytes/line (4 KiB total)
Allocated 8192 KiB SRAM memory, in 1 bank at 0x40000000
Allocated 32 MiB SDRAM memory, in 1 bank at 0x60000000
Allocated 8192 KiB ROM memory at 0x00000000
section: .text, addr: 0x40000000, size: 104400 bytes
section: .rodata, addr: 0x400197d0, size: 15616 bytes
section: .data, addr: 0x4001d4d0, size: 1176 bytes
read 1006 symbols


  Initializing and starting from 0x40000000
Hello, this is Rust!
PANIC: PanicInfo { payload: Any { .. }, message: Some(I am a panic), location: Location { file: "src/main.rs", line: 33, col: 5 }, can_unwind: true }

  Program exited normally on CPU 0.

Does the target support running the Rust testsuite?

I don't think so, the testsuite requires libstd IIRC.

Cross-compilation toolchains and C code

Does the target support C code?

Yes.

If so, what toolchain target should users use to build compatible C code? (This may match the target triple, or it may be a toolchain for a different target triple, potentially with specific options or caveats.)

I suggest BCC2 from Gaisler. It comes in both GCC and Clang variants.

@rustbot
Copy link
Collaborator

rustbot commented Jul 10, 2023

r? @jackh726

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Jul 10, 2023
@rustbot
Copy link
Collaborator

rustbot commented Jul 10, 2023

These commits modify compiler targets.
(See the Target Tier Policy.)

@rust-log-analyzer

This comment has been minimized.

@jonathanpallant
Copy link
Contributor Author

tidy error: /checkout/src/doc/rustc/src/platform-support/sparc-unknown-none-elf.md: missing trailing newline

Fixed.

@rust-log-analyzer

This comment has been minimized.

@jonathanpallant
Copy link
Contributor Author

error: language item required, but not found: eh_personality

Ignored example Rust code in sparc-unknown-none-elf.md because it doesn't compile on the default target.

@rust-log-analyzer

This comment has been minimized.

@jonathanpallant
Copy link
Contributor Author

unexplained "```ignore" doctest; try one:

OK, I explained why the test is ignored.

@rust-log-analyzer

This comment has been minimized.

@jonathanpallant
Copy link
Contributor Author

OK, I don't think the build failures are my fault?

@Urgau
Copy link
Member

Urgau commented Jul 11, 2023

OK, I don't think the build failures are my fault?

Try rebasing over the latest master branch. I think there was any issue the concurrency key with GHA.

Tested with the Gaisler bcc2 toolchain (both gcc and clang) and the Leon3 simulator.
@jonathanpallant
Copy link
Contributor Author

Rebased

@thejpster
Copy link

@jackh726 can I do anything else to help here?

Copy link
Member

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Now that's a name I haven't heard in a long time."

Comment on lines 27 to 50
By default, code generated with this target should run on any `SPARC` hardware;
enabling additional target features may raise this baseline.

- `-Ctarget-cpu=v8` adds the extra SPARC V8 instructions.

- `-Ctarget-cpu=leon3` adds the SPARC V8 instructions and sets up scheduling to
suit the Gaisler Leon3.

Functions marked `extern "C"` use the [standard SPARC architecture calling
convention](https://sparc.org/technical-documents/).

This target generates ELF binaries. Any alternate formats or special
considerations for binary layout will require linker options or linker scripts.

## Building the target

You can build Rust with support for the target by adding it to the `target`
list in `config.toml`:

```toml
[build]
build-stage = 1
target = ["sparc-unknown-none-elf"]
```
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-critical: Does using cargo +nightly build -Zbuild-std=core,alloc,compiler_builtins --release work? If so, it may be preferable in lieu of building a compiler with such support, if the v8 instructions and Leon3 scheduling is a significant difference, as that will rebuild std with the relevant instructions.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only tried with build-std=core but that's how I've been using it (rather than having the toolchain build the libcore in advance).

I suppose you could make this sparcv8-unknown-none-elf because I think SPARC V7 stuff is very very niche at this point.

Copy link
Member

@workingjubilee workingjubilee Jul 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, then I think we should document a recommendation to use -Zbuild-std if that's what the target's maintainer is preferentially using. ^^; No opinion on the arch version question.

@jackh726
Copy link
Member

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jul 17, 2023

📌 Commit 4bccf83 has been approved by jackh726

It is now in the queue for this repository.

@bors bors removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jul 17, 2023
@bors bors added the S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. label Jul 17, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 17, 2023
…r=jackh726

Add a sparc-unknown-none-elf target.

# `sparc-unknown-none-elf`

**Tier: 3**

Rust for bare-metal 32-bit SPARC V7 and V8 systems, e.g. the Gaisler LEON3.

## Target maintainers

- Jonathan Pallant, `[email protected]`, https://ferrous-systems.com

## Requirements

> Does the target support host tools, or only cross-compilation?

Only cross-compilation.

> Does the target support std, or alloc (either with a default allocator, or if the user supplies an allocator)?

Only tested with `libcore` but I see no reason why you couldn't also support `liballoc`.

> Document the expectations of binaries built for the target. Do they assume
specific minimum features beyond the baseline of the CPU/environment/etc? What
version of the OS or environment do they expect?

Tested by linking with a standard SPARC bare-metal toolchain - specifically I used the [BCC2] toolchain from Gaisler (both GCC and clang variants, both pre-compiled for x64 Linux and compiling my own SPARC GCC from source to run on `aarch64-apple-darwin`).

The target is set to use the lowest-common-denominator `SPARC V7` architecture (yes, they started at V7 - see [Wikipedia](https://en.wikipedia.org/wiki/SPARC#History)).

[BCC2]: https://www.gaisler.com/index.php/downloads/compilers

> Are there notable `#[target_feature(...)]` or `-C target-feature=` values that
programs may wish to use?

`-Ctarget-cpu=v8` adds the instructions added in V8.

`-Ctarget-cpu=leon3` adds the V8 instructions and sets up scheduling to suit the Gaisler LEON3.

> What calling convention does `extern "C"` use on the target?

I believe this is defined by the SPARC architecture reference manuals and V7, V8 and V9 are all compatible.

> What format do binaries use by default? ELF, PE, something else?

ELF

## Building the target

> If Rust doesn't build the target by default, how can users build it? Can users
just add it to the `target` list in `config.toml`?

Yes. I did:

```toml
target = ["aarch64-apple-darwin", "sparc-unknown-none-elf"]
```

## Building Rust programs

> Rust does not yet ship pre-compiled artifacts for this target. To compile for
this target, you will either need to build Rust with the target enabled (see
"Building the target" above), or build your own copy of `core` by using
`build-std` or similar.

Correct.

## Testing

> Does the target support running binaries, or do binaries have varying
expectations that prevent having a standard way to run them?

No - it's a bare metal platform.

> If users can run binaries, can they do so in some common emulator, or do they need native
hardware?

But if you use [BCC2] as the linker, you get default memory map suitable for the LEON3, and a default BSP for the LEON3, and so you can run the binaries in the `tsim-leon3` simulator from Gaisler.

```console
$ cat .cargo/config.toml | grep runner
runner = "tsim-leon3 -c sim-commands.txt"
$ cat sim-commands.txt
run
quit
$ cargo +sparcrust run --targe=sparc-unknown-none-elf
   Compiling sparc-demo-rust v0.1.0 (/work/sparc-demo-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 3.44s
     Running `tsim-leon3 -c sim-commands.txt target/sparc-unknown-none-elf/debug/sparc-demo-rust`

 TSIM3 LEON3 SPARC simulator, version 3.1.9 (evaluation version)

 Copyright (C) 2023, Frontgrade Gaisler - all rights reserved.
 This software may only be used with a valid license.
 For latest updates, go to https://www.gaisler.com/
 Comments or bug-reports to [email protected]

 This TSIM evaluation version will expire 2023-11-28

Number of CPUs: 2
system frequency: 50.000 MHz
icache: 1 * 4 KiB, 16 bytes/line (4 KiB total)
dcache: 1 * 4 KiB, 16 bytes/line (4 KiB total)
Allocated 8192 KiB SRAM memory, in 1 bank at 0x40000000
Allocated 32 MiB SDRAM memory, in 1 bank at 0x60000000
Allocated 8192 KiB ROM memory at 0x00000000
section: .text, addr: 0x40000000, size: 104400 bytes
section: .rodata, addr: 0x400197d0, size: 15616 bytes
section: .data, addr: 0x4001d4d0, size: 1176 bytes
read 1006 symbols

  Initializing and starting from 0x40000000
Hello, this is Rust!
PANIC: PanicInfo { payload: Any { .. }, message: Some(I am a panic), location: Location { file: "src/main.rs", line: 33, col: 5 }, can_unwind: true }

  Program exited normally on CPU 0.
```

> Does the target support running the Rust testsuite?

I don't think so, the testsuite requires `libstd` IIRC.

## Cross-compilation toolchains and C code

> Does the target support C code?

Yes.

> If so, what toolchain target should users use to build compatible C code? (This may match the target triple, or it may be a toolchain for a different target triple, potentially with specific options or caveats.)

I suggest [BCC2] from Gaisler. It comes in both GCC and Clang variants.
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 17, 2023
…r=jackh726

Add a sparc-unknown-none-elf target.

# `sparc-unknown-none-elf`

**Tier: 3**

Rust for bare-metal 32-bit SPARC V7 and V8 systems, e.g. the Gaisler LEON3.

## Target maintainers

- Jonathan Pallant, `[email protected]`, https://ferrous-systems.com

## Requirements

> Does the target support host tools, or only cross-compilation?

Only cross-compilation.

> Does the target support std, or alloc (either with a default allocator, or if the user supplies an allocator)?

Only tested with `libcore` but I see no reason why you couldn't also support `liballoc`.

> Document the expectations of binaries built for the target. Do they assume
specific minimum features beyond the baseline of the CPU/environment/etc? What
version of the OS or environment do they expect?

Tested by linking with a standard SPARC bare-metal toolchain - specifically I used the [BCC2] toolchain from Gaisler (both GCC and clang variants, both pre-compiled for x64 Linux and compiling my own SPARC GCC from source to run on `aarch64-apple-darwin`).

The target is set to use the lowest-common-denominator `SPARC V7` architecture (yes, they started at V7 - see [Wikipedia](https://en.wikipedia.org/wiki/SPARC#History)).

[BCC2]: https://www.gaisler.com/index.php/downloads/compilers

> Are there notable `#[target_feature(...)]` or `-C target-feature=` values that
programs may wish to use?

`-Ctarget-cpu=v8` adds the instructions added in V8.

`-Ctarget-cpu=leon3` adds the V8 instructions and sets up scheduling to suit the Gaisler LEON3.

> What calling convention does `extern "C"` use on the target?

I believe this is defined by the SPARC architecture reference manuals and V7, V8 and V9 are all compatible.

> What format do binaries use by default? ELF, PE, something else?

ELF

## Building the target

> If Rust doesn't build the target by default, how can users build it? Can users
just add it to the `target` list in `config.toml`?

Yes. I did:

```toml
target = ["aarch64-apple-darwin", "sparc-unknown-none-elf"]
```

## Building Rust programs

> Rust does not yet ship pre-compiled artifacts for this target. To compile for
this target, you will either need to build Rust with the target enabled (see
"Building the target" above), or build your own copy of `core` by using
`build-std` or similar.

Correct.

## Testing

> Does the target support running binaries, or do binaries have varying
expectations that prevent having a standard way to run them?

No - it's a bare metal platform.

> If users can run binaries, can they do so in some common emulator, or do they need native
hardware?

But if you use [BCC2] as the linker, you get default memory map suitable for the LEON3, and a default BSP for the LEON3, and so you can run the binaries in the `tsim-leon3` simulator from Gaisler.

```console
$ cat .cargo/config.toml | grep runner
runner = "tsim-leon3 -c sim-commands.txt"
$ cat sim-commands.txt
run
quit
$ cargo +sparcrust run --targe=sparc-unknown-none-elf
   Compiling sparc-demo-rust v0.1.0 (/work/sparc-demo-rust)
    Finished dev [unoptimized + debuginfo] target(s) in 3.44s
     Running `tsim-leon3 -c sim-commands.txt target/sparc-unknown-none-elf/debug/sparc-demo-rust`

 TSIM3 LEON3 SPARC simulator, version 3.1.9 (evaluation version)

 Copyright (C) 2023, Frontgrade Gaisler - all rights reserved.
 This software may only be used with a valid license.
 For latest updates, go to https://www.gaisler.com/
 Comments or bug-reports to [email protected]

 This TSIM evaluation version will expire 2023-11-28

Number of CPUs: 2
system frequency: 50.000 MHz
icache: 1 * 4 KiB, 16 bytes/line (4 KiB total)
dcache: 1 * 4 KiB, 16 bytes/line (4 KiB total)
Allocated 8192 KiB SRAM memory, in 1 bank at 0x40000000
Allocated 32 MiB SDRAM memory, in 1 bank at 0x60000000
Allocated 8192 KiB ROM memory at 0x00000000
section: .text, addr: 0x40000000, size: 104400 bytes
section: .rodata, addr: 0x400197d0, size: 15616 bytes
section: .data, addr: 0x4001d4d0, size: 1176 bytes
read 1006 symbols

  Initializing and starting from 0x40000000
Hello, this is Rust!
PANIC: PanicInfo { payload: Any { .. }, message: Some(I am a panic), location: Location { file: "src/main.rs", line: 33, col: 5 }, can_unwind: true }

  Program exited normally on CPU 0.
```

> Does the target support running the Rust testsuite?

I don't think so, the testsuite requires `libstd` IIRC.

## Cross-compilation toolchains and C code

> Does the target support C code?

Yes.

> If so, what toolchain target should users use to build compatible C code? (This may match the target triple, or it may be a toolchain for a different target triple, potentially with specific options or caveats.)

I suggest [BCC2] from Gaisler. It comes in both GCC and Clang variants.
bors added a commit to rust-lang-ci/rust that referenced this pull request Jul 17, 2023
…iaskrgr

Rollup of 4 pull requests

Successful merges:

 - rust-lang#112741 (fix typo in `rustdoc/src/what-is-rustdoc.md`)
 - rust-lang#113535 (Add a sparc-unknown-none-elf target.)
 - rust-lang#113651 (self type param infer, avoid ICE)
 - rust-lang#113770 (Generate safe stable code for derives on empty enums)

r? `@ghost`
`@rustbot` modify labels: rollup
@thejpster
Copy link

Did I break it with the extra commit? Sorry about that.

@workingjubilee
Copy link
Member

Sortof, d30294e and 4bccf83 still merged but none after that.

@thejpster
Copy link

Ok, I'll do a second PR with the text updates then?

@jackh726
Copy link
Member

I'm going to close this since a couple commits got merged. Weird state, but let's not make it any more weird. Please make another PR for changes that didn't get merged.

@jackh726 jackh726 closed this Jul 23, 2023
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Jul 24, 2023
…wn-readme, r=Amanieu

Update sparc-unknown-none-elf platform README

Cherry picked a couple of commits that didn't quite make it in rust-lang#113535
@jonathanpallant jonathanpallant deleted the sparc-bare-metal branch August 21, 2023 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants