semihosting.cc (12533:a5b047f55eb6) semihosting.cc (12698:cef1e0e7a368)
1/*
2 * Copyright (c) 2018 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software

--- 107 unchanged lines hidden (view full) ---

116};
117
118
119const std::vector<uint8_t> ArmSemihosting::features{
120 0x53, 0x48, 0x46, 0x42, // Magic
121 0x3, // EXT_EXIT_EXTENDED, EXT_STDOUT_STDERR
122};
123
1/*
2 * Copyright (c) 2018 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software

--- 107 unchanged lines hidden (view full) ---

116};
117
118
119const std::vector<uint8_t> ArmSemihosting::features{
120 0x53, 0x48, 0x46, 0x42, // Magic
121 0x3, // EXT_EXIT_EXTENDED, EXT_STDOUT_STDERR
122};
123
124const std::map<const std::string, FILE *> ArmSemihosting::stdioMap{
125 {"cin", ::stdin},
126 {"stdin", ::stdin},
127 {"cout", ::stdout},
128 {"stdout", ::stdout},
129 {"cerr", ::stderr},
130 {"stderr", ::stderr},
131};
132
124ArmSemihosting::ArmSemihosting(const ArmSemihostingParams *p)
125 : SimObject(p),
126 cmdLine(p->cmd_line),
127 memReserve(p->mem_reserve),
128 stackSize(p->stack_size),
129 timeBase([p]{ struct tm t = p->time; return mkutctime(&t); }()),
130 tickShift(calcTickShift()),
133ArmSemihosting::ArmSemihosting(const ArmSemihostingParams *p)
134 : SimObject(p),
135 cmdLine(p->cmd_line),
136 memReserve(p->mem_reserve),
137 stackSize(p->stack_size),
138 timeBase([p]{ struct tm t = p->time; return mkutctime(&t); }()),
139 tickShift(calcTickShift()),
131 semiErrno(0)
140 semiErrno(0),
141 stdin(getSTDIO("stdin", p->stdin, "r")),
142 stdout(getSTDIO("stdout", p->stdout, "w")),
143 stderr(p->stderr == p->stdout ?
144 stdout : getSTDIO("stderr", p->stderr, "w"))
132{
133 // Create an empty place-holder file for position 0 as semi-hosting
134 // calls typically expect non-zero file handles.
135 files.push_back(nullptr);
136
137 if (tickShift > 0)
138 inform("Semihosting: Shifting elapsed ticks by %i bits.",
139 tickShift);

--- 536 unchanged lines hidden (view full) ---

676 auto it = calls.find(op);
677 if (it == calls.end())
678 return nullptr;
679 else {
680 return &it->second;
681 }
682}
683
145{
146 // Create an empty place-holder file for position 0 as semi-hosting
147 // calls typically expect non-zero file handles.
148 files.push_back(nullptr);
149
150 if (tickShift > 0)
151 inform("Semihosting: Shifting elapsed ticks by %i bits.",
152 tickShift);

--- 536 unchanged lines hidden (view full) ---

689 auto it = calls.find(op);
690 if (it == calls.end())
691 return nullptr;
692 else {
693 return &it->second;
694 }
695}
696
697FILE *
698ArmSemihosting::getSTDIO(const char *stream_name,
699 const std::string &name, const char *mode)
700{
701 auto it = stdioMap.find(name);
702 if (it == stdioMap.end()) {
703 FILE *f = fopen(name.c_str(), mode);
704 if (!f) {
705 fatal("Failed to open %s (%s): %s\n",
706 stream_name, name, strerror(errno));
707 }
708 return f;
709 } else {
710 return it->second;
711 }
712}
713
684std::unique_ptr<ArmSemihosting::FileBase>
685ArmSemihosting::FileBase::create(
686 ArmSemihosting &parent, const std::string &fname, const char *mode)
687{
688 std::unique_ptr<FileBase> file;
689 if (fname == ":semihosting-features") {
690 file.reset(new FileFeatures(parent, fname.c_str(), mode));
691 } else {

--- 122 unchanged lines hidden (view full) ---

814
815int64_t
816ArmSemihosting::File::openImpl(bool in_cpt)
817{
818 panic_if(file, "Trying to open an already open file.\n");
819
820 if (_name == ":tt") {
821 if (mode[0] == 'r') {
714std::unique_ptr<ArmSemihosting::FileBase>
715ArmSemihosting::FileBase::create(
716 ArmSemihosting &parent, const std::string &fname, const char *mode)
717{
718 std::unique_ptr<FileBase> file;
719 if (fname == ":semihosting-features") {
720 file.reset(new FileFeatures(parent, fname.c_str(), mode));
721 } else {

--- 122 unchanged lines hidden (view full) ---

844
845int64_t
846ArmSemihosting::File::openImpl(bool in_cpt)
847{
848 panic_if(file, "Trying to open an already open file.\n");
849
850 if (_name == ":tt") {
851 if (mode[0] == 'r') {
822 file = stdin;
852 file = parent.stdin;
823 } else if (mode[0] == 'w') {
853 } else if (mode[0] == 'w') {
824 file = stdout;
854 file = parent.stdout;
825 } else if (mode[0] == 'a') {
855 } else if (mode[0] == 'a') {
826 file = stderr;
856 file = parent.stderr;
827 } else {
828 warn("Unknown file mode for the ':tt' special file");
829 return -EINVAL;
830 }
831 } else {
832 std::string real_mode(this->mode);
833 // Avoid truncating the file if we are restoring from a
834 // checkpoint.

--- 17 unchanged lines hidden (view full) ---

852 file = nullptr;
853
854 return 0;
855}
856
857bool
858ArmSemihosting::File::isTTY() const
859{
857 } else {
858 warn("Unknown file mode for the ':tt' special file");
859 return -EINVAL;
860 }
861 } else {
862 std::string real_mode(this->mode);
863 // Avoid truncating the file if we are restoring from a
864 // checkpoint.

--- 17 unchanged lines hidden (view full) ---

882 file = nullptr;
883
884 return 0;
885}
886
887bool
888ArmSemihosting::File::isTTY() const
889{
860 return file == stdout || file == stderr || file == stdin;
890 return file == parent.stdout ||
891 file == parent.stderr ||
892 file == parent.stdin;
861}
862
863int64_t
864ArmSemihosting::File::read(uint8_t *buffer, uint64_t size)
865{
866 panic_if(!file, "Trying to read from a closed file");
867
868 size_t ret = fread(buffer, 1, size, file);

--- 95 unchanged lines hidden ---
893}
894
895int64_t
896ArmSemihosting::File::read(uint8_t *buffer, uint64_t size)
897{
898 panic_if(!file, "Trying to read from a closed file");
899
900 size_t ret = fread(buffer, 1, size, file);

--- 95 unchanged lines hidden ---